Hugh
2010-03-12 19:23:01 UTC
Hi there,
I use follow code to open a text file and then to append text to the file.
However, sometimes, it does not work the error is "file is being used by
another process". I tried to manually open the file by double click the
file, no problem. I am confused. the code works sometimes or for some files,
but not always. Please, help me. Why the code does not work? Thanks very
much in advance.
midland
Function OpenStreamWriter(ByVal filepath As String) As StreamWriter
Dim SW As StreamWriter = Nothing
Dim FI As New FileInfo(filepath)
Try
If FI.Exists = True Then
SW = File.AppendText(filepath)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
SW = Nothing
End Try
Return SW
End Function
I use follow code to open a text file and then to append text to the file.
However, sometimes, it does not work the error is "file is being used by
another process". I tried to manually open the file by double click the
file, no problem. I am confused. the code works sometimes or for some files,
but not always. Please, help me. Why the code does not work? Thanks very
much in advance.
midland
Function OpenStreamWriter(ByVal filepath As String) As StreamWriter
Dim SW As StreamWriter = Nothing
Dim FI As New FileInfo(filepath)
Try
If FI.Exists = True Then
SW = File.AppendText(filepath)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
SW = Nothing
End Try
Return SW
End Function