Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I have a problem during my vb programming, is that:
When my program read from a file I got an exception called "Unable to access path '' the file is used by another process". So, How can I get the process that use this file and pause its use until I finish reading this file after that resume the process?

Thanx.....
Posted

1 solution

First of all, pausing (suspending) of some process holding your file won't help you at all — the file will continue to be locked. In certain cases it could even create a deadlock. And you hardly want to disrupt some operation by terminating that process.

Second thing is: 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 same 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>


[EDIT]

This utility will scans 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
 
v5
Comments
[no name] 22-Apr-12 15:49pm    
Thanx, that was very helpful.
I will back to you if I had a problem.
Sergey Alexandrovich Kryukov 22-Apr-12 15:54pm    
Sure, you are very welcome.
Good luck, call again.
--SA

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