|
-
Oct 22nd, 2009, 04:27 AM
#1
Thread Starter
Junior Member
[RESOLVED] Open and create log file
Hi,
How can I open and create file in VB.NET as I do in VB6?
Dim lotno As String = "123456"
FileOpen(1, (Application.StartupPath & "\Log\" & lotno & ".txt"), OpenMode.Append)
PrintLine(1, "Start Time :", Now())
FileClose(1)
-
Oct 22nd, 2009, 04:41 AM
#2
Addicted Member
Re: Open and create log file
You can do this:
vb.net Code:
Dim fs As New FileStream("C:\somwhere\on\your\disk.log", FileMode.Create, FileAccess.Write) Dim s As New StreamWriter(fs, System.Text.Encoding.Default) s.BaseStream.Seek(0, SeekOrigin.End) s.WriteLine("Something") s.Close()
Just remember to import the System.IO namespace
-
Oct 22nd, 2009, 08:44 PM
#3
Thread Starter
Junior Member
Re: Open and create log file
hi erik,
thanks for ur reply..
but how can I use OpenMode.Append in vb.net coz I need to create a file in existing file witout deleting the old data in the file
-
Oct 22nd, 2009, 09:15 PM
#4
Addicted Member
Re: Open and create log file
maybe this will help
Code:
Public Sub WriteData(ByVal strData As String, ByVal dir As String, ByVal path As String, ByVal append As Boolean)
'Check directory first
If Not (System.IO.Directory.Exists(dir)) Then
System.IO.Directory.CreateDirectory(dir)
End If
Dim objReader As System.IO.StreamWriter
Try
objReader = New System.IO.StreamWriter(path, append)
objReader.Write("Time: " & Now() & " LOG: " & strData & vbCrLf)
'objReader.WriteLine(vbCrLf) 'uncomment for blank line between entries
objReader.Close()
Catch Ex As Exception
'Throw New Exception(Ex.Message)
End Try
End Sub
-
Oct 22nd, 2009, 09:23 PM
#5
Member
Re: Open and create log file
and this might also help... at least you have options
i actually have a log module in my application and im currently using this code.
I didn't import anything because i already specified the package in the declarations
Code:
Dim myFile As System.IO.File, _
fWriter As System.IO.StreamWriter, _
fName, fLog As String
'string to be written in the text file
fLog = Now.ToString("hh:mm tt") & vbTab & "LOGINFO" & vbTab & "userAction"
'file path and name
fName = "C:\Logs\ERROR_LOG_" & Now.ToString("ddMMyyyy") & ".txt"
'if file is existing already: the boolean parameter is set to true to append a new line on the file
If myFile.Exists(fName) Then
fWriter = New System.IO.StreamWriter(fName, True)
Else
'if it does not exist, create a new text
fWriter = myFile.CreateText(repFName)
End If
fWriter.WriteLine(fLog)
fWriter.Close()
fWriter = Nothing
 Originally Posted by shxrainz
hi erik,
thanks for ur reply..
but how can I use OpenMode.Append in vb.net coz I need to create a file in existing file witout deleting the old data in the file
Last edited by fantasy2ville; Oct 22nd, 2009 at 09:24 PM.
Reason: typo errors
-
Oct 22nd, 2009, 09:48 PM
#6
Thread Starter
Junior Member
Re: Open and create log file
hi all,
Thanks for the info.
I just find the solution by using FileMode.Append which it will append the text into the existing file and it work.
'Append to file : FileMode.Append
Dim fs As New FileStream((Application.StartupPath & "\Log\" & lotno & ".txt"), FileMode.Append, FileAccess.Write)
Dim s As New StreamWriter(fs, System.Text.Encoding.Default)
s.BaseStream.Seek(0, SeekOrigin.End)
s.WriteLine(countrows & " " & Now() & " " & mfgnum & " " & "PASS")
s.WriteLine(countrows & " " & Now() & " " & mfgnum & " " & "PASS")
s.Close()
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|