Discussion:
File is being used by another process
(too old to reply)
Hugh
2010-03-12 19:23:01 UTC
Permalink
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
Hugh
2010-03-12 19:41:01 UTC
Permalink
One more detail: the error pops up at "SW = File.AppendText(filepath)".
Post by Hugh
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
Wilson, Phil
2010-03-12 22:42:43 UTC
Permalink
It depends how the other program opened the file. In Win32 a file created
with FILE_SHARE_WRITE means that other processes cannot write to it. In
.NET opening with FileMode.ReadWrite probably has the same effect. At the
risk of stating the obvious, you can't go writing all over another file at
the same time as some other process.
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972
Post by Hugh
One more detail: the error pops up at "SW = File.AppendText(filepath)".
Post by Hugh
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
Hugh
2010-03-15 13:40:01 UTC
Permalink
Phil,

Thanks very much for your response. No other process accesses the file. As
a matter of fact, I was able to manually open the file just before the error
popped up in "SW = File.AppendText(filepath)" without any problem.

Thanks again.
Post by Wilson, Phil
It depends how the other program opened the file. In Win32 a file created
with FILE_SHARE_WRITE means that other processes cannot write to it. In
.NET opening with FileMode.ReadWrite probably has the same effect. At the
risk of stating the obvious, you can't go writing all over another file at
the same time as some other process.
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972
Post by Hugh
One more detail: the error pops up at "SW = File.AppendText(filepath)".
Post by Hugh
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
Wilson, Phil
2010-03-15 16:53:11 UTC
Permalink
If you are absolutely certain that no other process has the file open, it's
possible that you have it open already. AppendText is documented to create a
StreamWriter to do this, so maybe you already opened it.
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972
Post by Hugh
Phil,
Thanks very much for your response. No other process accesses the file.
As
a matter of fact, I was able to manually open the file just before the error
popped up in "SW = File.AppendText(filepath)" without any problem.
Thanks again.
Post by Wilson, Phil
It depends how the other program opened the file. In Win32 a file created
with FILE_SHARE_WRITE means that other processes cannot write to it. In
.NET opening with FileMode.ReadWrite probably has the same effect. At the
risk of stating the obvious, you can't go writing all over another file at
the same time as some other process.
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972
Post by Hugh
One more detail: the error pops up at "SW =
File.AppendText(filepath)".
Post by Hugh
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
Hugh
2010-03-15 21:04:01 UTC
Permalink
This post might be inappropriate. Click to display it.
Loading...