Tutorial :Issues about files or folders in use: get the name of another Process that use file or folder


I using C# .NET , vs 2008 , .net 3.5

For me, is difficult, but I need sample code in C# for this:

  1. Check if a file or a folder is in use

  2. If file or a folder is in use, the name of Process that use it

For example, in my issue.

I try delete file, and I get "The process cannot access the file 'XYZ' because it is being used by another process." Exception.


I want check if a file is in use, and the name of Process that use it.

I need sample code, source code, please. I dont want use c++, I dont know c, c++, unmanaged code, or WinApi. I want use only C# code (managed code .net).

I have read several references but not get sample code source,

How to check if a file is in use?

Emulate waiting on File.Open in C# when file is locked


How to check if a file is in use?

Easiest way to read text file which is locked by another application

Using C# is it possible to test if a lock is held on a file

EDIT: From Yan Jun - MSFT

  string path = "D:\\temp2.xlsx";              foreach (Process c in Process.GetProcesses()) {                    if (c.MainWindowTitle.Contains(Path.GetFileName(path))){                      MessageBox.Show(c.ProcessName);                      return;                  }              }                try{                  FileInfo f = new FileInfo(path);                    f.Delete();              }              catch (Exception ex){                    MessageBox.Show(ex.Message);              }  

... But it is difficult get solution for all 100% issues.

  1. Problem if c.MainWindowTitle == null or not contains filename.

  2. Problem for shared folder in another machine, PC, server,... like:


any sample code, I ask for help gurus, MVPs, anyone.

UPDATE: the same issue for a folder


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.

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 (Unlocker being the best known example). You can also use ProcessExplorer to search for open file handles by the filename. Those don't really help you though.

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?

EDIT: You could shell out to sysinternals Handle 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

string result = proc.StandardOutput.ReadToEnd();  

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...

If you're still interested, I can show you how you'd go about this.

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.

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: (?<pid>[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();          }      }  }  


There is no purely managed way to do this. You have to use some low-level APIs through P/invoke or similar.

There's good information here on a way to do it, but it's C++ code. You'd have to do the porting yourself.


Note there are some complex issues with this, namely the issues around kernel vs. userspace memory. This is not a simple problem you're trying to solve.


Try the windows Process Explorer:


Won't let you do it from code, but at least you can figure out what the source of your locks are.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »