This is not your main problem; you main problem is understanding. The notion of "Form" is related to UI. How a UI can be related to such thing as file/stream operations? You need to embrace
separation of concerns.
Now, let's get to the problem of the file access.
It depends on how you opened the file. Most likely,
you forgot to dispose something. The disposal mechanism usually takes care of certain things you need to clean up in a controlled manner (in contrast to, say,
Garbage Collector functionality), in some point of execution of your code; and some typical uses of it are cleaning up unmanaged resources and — what a surprise! — closing of file handles.
Basically, you need to watch yourself of using of any types implementing
System.IDisposable
and call
System.IDisposable.Dispose
in the appropriate points. Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[
^].
It's important to makes sure this call is done even if exception is thrown, via a try-finally statement. The best fool-proof way of doing it is using the
using
statement (don't mix it up with
using
clause!). Please see:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[
^].
Let's consider a simple example. When you open a file with
System.IO.StreamReader
, you do it in the exclusive-use mode (which is the best thing to do anyway). You won't be able to read the same file in parallel until the instance of the reader is disposed:
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName)) {
string line = reader.ReadLine();
}
You could have used different ways of accessing your files, you could get read, write, read/write access, work on lower levels of file or stream interfaces, etc., but the principle is the same.
Where do you have this problem? This is easy to investigate.
First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up, as explained above.
In some cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the
Sysinternals Suite. This set of utilities (formerly from
Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[
^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[
^].
The utility you need is "
handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[
^].
In your case, you use it with file name parameter:
handle.exe <file_name>
This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its
pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its
Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[
^].
Good luck,
—SA