J
jtertin
I am currently using the following code to make sure a file is
writable (i.e. is not in use) by using the CanWrite method of the
FileStream object. Using the code below, the TextWriter is still
trying to write to the file and is throwing an exception (File... in
use by another process) whenever the output file (strFileName) is
open. I am trying to prevent it from writing to it at all if this is
the case... Any insight?
Public Shared Sub Log(ByVal logMessage As String)
Dim strFileName As New FileStream(strLogFile, FileMode.Append,
FileAccess.Write, FileShare.None)
If strFileName.CanWrite Then
Dim w As StreamWriter = New StreamWriter(strFileName)
w.WriteLine(DateTime.Now.ToShortDateString & vbTab &
DateTime.Now.ToLongTimeString & vbTab & logMessage)
w.Flush()
w.Close()
End If
End Sub
writable (i.e. is not in use) by using the CanWrite method of the
FileStream object. Using the code below, the TextWriter is still
trying to write to the file and is throwing an exception (File... in
use by another process) whenever the output file (strFileName) is
open. I am trying to prevent it from writing to it at all if this is
the case... Any insight?
Public Shared Sub Log(ByVal logMessage As String)
Dim strFileName As New FileStream(strLogFile, FileMode.Append,
FileAccess.Write, FileShare.None)
If strFileName.CanWrite Then
Dim w As StreamWriter = New StreamWriter(strFileName)
w.WriteLine(DateTime.Now.ToShortDateString & vbTab &
DateTime.Now.ToLongTimeString & vbTab & logMessage)
w.Flush()
w.Close()
End If
End Sub