Writing text to file

J

John

Hi

I am trying to set-up an app log file that needs to be created if does not
exists but opened for append if exists. How does one go about doing this?
..net seems to have many ways to do io stream, boggles the mind.

Thanks

Regards
 
M

Mike Lowery

John said:
Hi

I am trying to set-up an app log file that needs to be created if does not
exists but opened for append if exists. How does one go about doing this? .net
seems to have many ways to do io stream, boggles the mind.

If File.Exists("c:\myfile.txt") Then
'append
Else
'create
End If
 
J

John

Hi

I have done this much already;

Dim JobsFS As FileStream
If File.Exists(JobsFSPath) Then
JobsFS = File.Open(JobsFSPath, FileMode.Append)
Else
JobsFS = File.Create(JobsFSPath)
End If

What I don't understand is how to write some text into it, like
JobsFS.Write("This is some text") or something similar.

Thanks

Regards
 
A

Andrew Morton

John said:
I have done this much already;

Dim JobsFS As FileStream
If File.Exists(JobsFSPath) Then
JobsFS = File.Open(JobsFSPath, FileMode.Append)
Else
JobsFS = File.Create(JobsFSPath)
End If

What I don't understand is how to write some text into it, like
JobsFS.Write("This is some text") or something similar.

Imports System.IO
Dim fsw As New FileStream(currentLog, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.Read)
fsw.Position = fsw.Length
Dim w As StreamWriter = New StreamWriter(fsw)
w.WriteLine(DateTime.Now.ToLongTimeString() & vbTab & s)
w.Close()

where currentLog is the path-and-filename of the file to create/append to
and the variable s is the message to write.

HTH

Andrew
 
M

Miro

Is something like this you are looking for ?
-A simple function that you pass in some text. It makes sure the file is
there, if not it creates it and then "APPENDS" the
text to the end of the file.
=================
Public Function WriteTextFile(ByVal ExtraString As String)
Dim FileName As String = "Text.txt"
Dim oTextFile As System.IO.File
Dim oTextWriteText As System.IO.StreamWriter
'Check if file exists...if not create it.
If Not System.IO.File.Exists(FileName) Then
oTextWriteText = oTextFile.CreateText(FileName)
oTextWriteText.Close()
End If

'If something passed in
If Not ExtraString = String.Empty Then
'Blank Line between last line.
oTextWriteText.WriteLine()
oTextWriteText.WriteLine( ExtraString )
oTextWriteText.WriteLine()
End If
'Closes the Text File.
oTextWriteText.Close()
End Function
=================


Miro
 
H

HKSHK

Hi John,

If you use the StreamWriter class, you don't have to worry if the file
exists or not.

Dim path As String = "c:\temp\MyTest.txt"

Dim sw As New StreamWriter(path, True)
sw.WriteLine("This")
sw.WriteLine("is a test.")
sw.Flush()
sw.Close()

Best Regards,

HKSHK
 
O

Oenone

John said:
I am trying to set-up an app log file that needs to be created if
does not exists but opened for append if exists. How does one go
about doing this? .net seems to have many ways to do io stream,
boggles the mind.

If you're using VS2005, you can solve this using the much simpler
System.IO.File.AppendAllText() method. For example:

\\\
IO.File.AppendAllText(Filename, Content)
///

This will create the file specified in Filename if it doesn't already exist,
or append to it if it does. The contents of the string variable Content will
then be added to the end of the file.

HTH,
 
G

Greg

Hi

I am trying to set-up an app log file that needs to be created if does not
exists but opened for append if exists. How does one go about doing this?
.net seems to have many ways to do io stream, boggles the mind.

Thanks

Regards

Hi John,
I use something like the following sub to write to my log file.
If the file doesn't exist then the Append command will create it anyway.
In the code, I'd call sbLog ("Commenced reading input file") etc.

Private Sub sbLog(ByVal sMsg As String)

'Write entry to the log file
Dim sFilename As String
Dim sData As String
Dim iFileNo As Integer

sFilename = "C:\LogFiles\" & Application.ProductName & ".log"
sData = Format(Now, "Short Time") & " " & sMsg
iFileNo = FreeFile()
FileOpen(iFileNo, sFilename, OpenMode.Append)
Print(iFileNo, sData & vbCrLf)
FileClose(iFileNo)

End Sub

Cheers,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top