Home All Groups Group Topic Archive Search About
Author
29 Aug 2006 3:40 PM
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

Author
29 Aug 2006 3:50 PM
Mike Lowery
"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
Author
29 Aug 2006 4:14 PM
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


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
>
>
Author
29 Aug 2006 4:28 PM
Andrew Morton
John wrote:
> 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
Author
29 Aug 2006 4:37 PM
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







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
>>
>>
>
>
Author
29 Aug 2006 6:05 PM
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

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
>
>
Author
29 Aug 2006 6:26 PM
Oenone
John wrote:
> 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,

--

(O)enone
Author
30 Aug 2006 8:34 PM
ShaneO
Oenone wrote:
Show quoteHide quote
> John wrote:
>> 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,
>
In a similar way as Oenone, I use the following -

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.
Author
30 Aug 2006 1:05 AM
Greg
"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.
>
> 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