Stream Writer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

IF i want to write a line at the same time in append mode how m i suppose to
do.

here is my code.... and this function may be called more then once at the
same time....

and it works fine... but the only thing is that.. sometime i get the error :

The process cannot access the file "c:\nie\LOGS\1062005.txt" because it is
being used by another process.

and i guess this is true as this function is getting called more then once
at the sametime on the same thread and its trying to over lap.

below is the function

Public Function WriteServiceLog(ByVal EventCode As String, ByVal Details As
String)
Dim Path As String = AppDomain.CurrentDomain.BaseDirectory & "\LOGS\"
Dim FileName As String = Replace(FormatDateTime(Date.Now,
DateFormat.ShortDate), "/", "") & ".txt"
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
End If
'Dim FStream As New System.IO.FileStream(Path & FileName,
IO.FileMode.Append, IO.FileAccess.ReadWrite)

Dim StreamWriter As New System.IO.StreamWriter(Path & FileName, True)

StreamWriter.WriteLine(EventCode & " " & FormatDateTime(Date.Now,
DateFormat.LongTime) & " " & Details)
StreamWriter.Close()


End Function

Any help?

Cheers
C
 
Gujju said:
and i guess this is true as this function is getting called more then once
at the sametime on the same thread and its trying to over lap.

It won't overlap if it's called on the same thread.
 
Hi,

Try using synclock to prevent more than one of the functions
from running at a time. It is not the best idea to use the type name as the
variable name.

Public Function WriteServiceLog(ByVal EventCode As String, ByVal Details As
String)
Dim Path As String = AppDomain.CurrentDomain.BaseDirectory & "\LOGS\"
Dim FileName As String = Replace(FormatDateTime(Date.Now,
DateFormat.ShortDate), "/", "") & ".txt"
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
End If
'Dim FStream As New System.IO.FileStream(Path & FileName,
IO.FileMode.Append, IO.FileAccess.ReadWrite)
synclock filename
Dim SW As New System.IO.StreamWriter(Path & FileName, True)

SW.WriteLine(EventCode & " " & FormatDateTime(Date.Now,
DateFormat.LongTime) & " " & Details)
SW.Close()
end synclock

End Function

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmsynclock.asp

Ken
--------------------------
Hi,

IF i want to write a line at the same time in append mode how m i suppose to
do.

here is my code.... and this function may be called more then once at the
same time....

and it works fine... but the only thing is that.. sometime i get the error :

The process cannot access the file "c:\nie\LOGS\1062005.txt" because it is
being used by another process.

and i guess this is true as this function is getting called more then once
at the sametime on the same thread and its trying to over lap.

below is the function

Public Function WriteServiceLog(ByVal EventCode As String, ByVal Details As
String)
Dim Path As String = AppDomain.CurrentDomain.BaseDirectory & "\LOGS\"
Dim FileName As String = Replace(FormatDateTime(Date.Now,
DateFormat.ShortDate), "/", "") & ".txt"
If Not System.IO.Directory.Exists(Path) Then
System.IO.Directory.CreateDirectory(Path)
End If
'Dim FStream As New System.IO.FileStream(Path & FileName,
IO.FileMode.Append, IO.FileAccess.ReadWrite)

Dim StreamWriter As New System.IO.StreamWriter(Path & FileName, True)

StreamWriter.WriteLine(EventCode & " " & FormatDateTime(Date.Now,
DateFormat.LongTime) & " " & Details)
StreamWriter.Close()


End Function

Any help?

Cheers
C
 
Back
Top