Patricia Kolb

Patricia Kolb

  • NA
  • 6
  • 3.1k

vb.net 2010 streamwriter

Aug 27 2012 5:22 PM

I have a subroutine called 'ErrorFileProcess'.  It takes in my error number, error message, and error file name which includes path, and writes that error message to the error file.  The sub 1st determines if the file already exists, and if so, opens with append, otherwise it creates the file.  When I first call the subroutine it works just fine, sees that there is NO error file so it creates it and writes out the error.  But when I call the routine again, it correctly sees that the file DOES exist, but when I try to open it for append the program errors and says that the file is locked by another process. 

ex.Message is: The process cannot access the file '\\ffgenv\docenv\FFG\DPBatch_Error.Log' because it is being used by another process.
Below is the code:

Public Sub ErrorFileProcess(ByVal strErrData As String, ByVal intMyErrNum As Integer, ByVal strFileNamePath As String)
blnErrorHit =
True
Dim strErrBeginText As String
Try
If System.IO.File.Exists(strFileNamePath) = True Then

Using writer As StreamWriter = New StreamWriter(strFileNamePath, True)
   writer.WriteLine(
"Err Msg: " & strErrData & " Err Num: " & intMyErrNum)
   writer.Close()
End Using

Else

Using writer As StreamWriter = New StreamWriter(strFileNamePath)

strErrBeginText =
"DocPrint Batch ERROR Report" & vbCrLf
writer.WriteLine(strErrBeginText)
strErrBeginText =
"Date Time Created: " & System.DateTime.Now & vbCrLf & vbCrLf
writer.WriteLine(strErrBeginText)
strErrBeginText =
"ERROR MESSAGE" & vbCrLf
writer.WriteLine(strErrBeginText)
writer.WriteLine(
"Err Msg: " & strErrData & " Err Num: " & intMyErrNum)
writer.Close()

End Using

End If
Catch ex As Exception

ErrorEmail(
"Exception from ErrorFileProcess where ex.Message is: " & ex.Message) 'this is the error module, its the end of the line, so all I can think to do is send email at this point

End Try

Answers (9)