Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's not going to be a way to find the process that has the file opened without stepping into the WinApi, I don't think. And as far as checking whether its in use, the only thing you can really do, as the SO questions you linked to state, is to wrap the file access attempts in a try/catch block. </p> <p>The code to find which file has it opened is likely to be ugly, but there may be an API out there that wraps this up nicely. There are 3rd party utilities that will tell you this (<a href="http://www.emptyloop.com/unlocker/" rel="nofollow noreferrer">Unlocker</a> being the best known example). You can also use <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow noreferrer">ProcessExplorer</a> to search for open file handles by the filename. Those don't really help you though. </p> <p>The short answer of what I'm trying to get across here is you have the answer for the first part of your question in the SO questions you already linked, and the second part would probably require WIN32 calls, which you want to avoid, but you're probably going to have to get your hands dirty in Win32... Still want help?</p> <p>EDIT: You could shell out to sysinternals <a href="http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx" rel="nofollow noreferrer">Handle</a> utility. You would need to get the output of that command and parse it yourself. You can read the executed process's output like this</p> <pre><code>string result = proc.StandardOutput.ReadToEnd(); </code></pre> <p>The issue with this is you're going to get a license agreement popup the first time you run the Handle utility. Not to mention the whole licensing issues if this is something you hope to deploy...</p> <p>If you're still interested, I can show you how you'd go about this.</p> <p>EDIT: Here's a runnable program that will find the exe name and pid of any program that has an open handle to a file. I added comments, but can elaborate further if necessary. I use Regular Expressions here to parse the output as that makes the most sense given the task at hand.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ProcessStartInfo si = new ProcessStartInfo(); si.FileName = "handle.exe"; //name of the handle program from sysinternals //assumes that its in the exe directory or in your path //environment variable //the following three lines are required to be able to read the output (StandardOutput) //and hide the exe window. si.RedirectStandardOutput = true; si.WindowStyle = ProcessWindowStyle.Hidden; si.UseShellExecute = false; si.Arguments = "test.xlsx"; //this is the file you're trying to access that is locked //these 4 lines create a process object, start it, then read the output to //a new string variable "s" Process p = new Process(); p.StartInfo = si; p.Start(); string s = p.StandardOutput.ReadToEnd(); //this will use regular expressions to search the output for process name //and print it out to the console window string regex = @"^\w*\.EXE"; MatchCollection matches = Regex.Matches(s, regex, RegexOptions.Multiline); foreach (var match in matches) { Console.WriteLine(match); } //this will use regex to search the output for the process id (pid) //and print it to the console window. regex = @"pid: (?&lt;pid&gt;[0-9]*)"; matches = Regex.Matches(s, regex, RegexOptions.Multiline); foreach (var obj in matches) { Match match = (Match)obj; //i have to cast to a Match object //to be able to get the named group out Console.WriteLine(match.Groups["pid"].Value.ToString()); } Console.Read(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload