|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Writing text to fileHi
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 "John" <John@nospam.infovis.co.uk> wrote in message If File.Exists("c:\myfile.txt") Thennews:%23nmQiF4yGHA.3584@TK2MSFTNGP02.phx.gbl... > 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. 'append Else 'create End If 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 Show quoteHide quote "Mike Lowery" <selfspam@mouse-potato.com> wrote in message news:%23vxHhL4yGHA.4408@TK2MSFTNGP05.phx.gbl... > > "John" <John@nospam.infovis.co.uk> wrote in message > news:%23nmQiF4yGHA.3584@TK2MSFTNGP02.phx.gbl... >> 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 > > John wrote:
> I have done this much already; Imports System.IO> > 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. 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 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 Show quoteHide quote "John" <John@nospam.infovis.co.uk> wrote in message news:O27b9Y4yGHA.4232@TK2MSFTNGP05.phx.gbl... > 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 > > > "Mike Lowery" <selfspam@mouse-potato.com> wrote in message > news:%23vxHhL4yGHA.4408@TK2MSFTNGP05.phx.gbl... >> >> "John" <John@nospam.infovis.co.uk> wrote in message >> news:%23nmQiF4yGHA.3584@TK2MSFTNGP02.phx.gbl... >>> 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 >> >> > > 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 John wrote: Show quoteHide quote > 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 > > John wrote:
> I am trying to set-up an app log file that needs to be created if If you're using VS2005, you can solve this using the much simpler > 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. 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 Oenone wrote:
Show quoteHide quote > John wrote: In a similar way as Oenone, I use the following ->> 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, > Sub LogFile(ByVal sMsg As String) sMsg &= vbCrLf My.Computer.FileSystem.WriteAllText(sFileName, sMsg, True) End Sub This also automatically handles creating or appending of the Log File and is a simple solution. ShaneO There are 10 kinds of people - Those who understand Binary and those who don't. "John" <John@nospam.infovis.co.uk> wrote in message Hi John,news:%23nmQiF4yGHA.3584@TK2MSFTNGP02.phx.gbl... > 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 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
try Catch for empty textfield
Passing System.Enum as a parameter type... Help! VB Express & Excel automation ? Crystal Reports for .NET 2005 secuity issue with terminal services connection? VB .Net (VisualStudio 2005) and the SQL Server SMO Object question Getting Data out of MS Project First Report in VB2005 - Print Preview moving controls with in an image Fade out problem on a form with irregular backgroud image Ole db Command |
|||||||||||||||||||||||