Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there is a C# formapp .
it creates and uses and delete files.
in deleting files i do not know which object or process is using them to dispose them .forcing garbage collection doesn't help too.
what would you do if you had this problem?
thank you in advance.
Posted
Comments
[no name] 25-Nov-14 14:16pm    
Most probably there is a way to find out which process is using a file because e.g. Process Explorer from Sysinternals can also do this.
If you can do the same you can try to kill the process, which is not possible in every case.
Anyway what you are trying to do will give you most probably other problems.
Therefore I think you should think about to find a better solution then this "brute force" method.

The first thing to do is to ensure that you always close and dispose all file related objects when you are finished with them: otherwise you will likely leave a lock on the file and won't be able to clear it.

If that doesn't solve your problem, then you need to know what handles are in use.
That's complicated to do in code, but Microsoft has done the work for you: just run Handle.Exe in a process and parse the results. http://technet.microsoft.com/en-gb/sysinternals/bb896655.aspx[^] - It's a clumsy looking approach, but it should work (you may need process elevation to use it).
Once you have the process, you can inform the user who is using the file, and get him to save his work and close the handle - I would strongly suggest that you don;t start killing processes yourself, given that most processes have the file open because they are using it, and will almost certainly lose data and annoy users if you kill processes before the user gets a chance to save...
 
Share this answer
 
If a file is in use, you cannot enforce deleting it. This is the very important protective feature of most modern OS. Without it, the system could not behave in a stable way. You can only kill the process holding the file and only then remove it. But don't do it. You need to do proper clean-up instead and make sure the processes close the files when they are not using (or, just the opposite, hold them to prevent deleting them and, optionally, the modifications).

Now, the question is: what process holds the file in question? 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. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900