all groups > vb.net data > august 2006 >
You're in the

vb.net data

group:

Writing text to file


Re: Writing text to file Mike Lowery
8/29/2006 8:50:39 AM
vb.net data:

[quoted text, click to view]

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

Re: Writing text to file Miro
8/29/2006 12:37:03 PM
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







[quoted text, click to view]

Writing text to file John
8/29/2006 4:40:00 PM
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

Re: Writing text to file John
8/29/2006 5:14:46 PM
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


[quoted text, click to view]

Re: Writing text to file Andrew Morton
8/29/2006 5:28:30 PM
[quoted text, click to view]

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

Re: Writing text to file Oenone
8/29/2006 6:26:05 PM
[quoted text, click to view]

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,

--

(O)enone

Re: Writing text to file HKSHK
8/29/2006 8:05:21 PM
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

[quoted text, click to view]
Re: Writing text to file Greg
8/30/2006 12:00:00 AM
[quoted text, click to view]

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



AddThis Social Bookmark Button