Can anyone please tell me in C# how to find a particular file is loaded by which processes? For example I want to check if "C:\test.doc" is loaded by which processes.
Thanks,
Mushq
Mushq
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Apr 29, 2008, 7:17 PM
You can do it in a fashion with code such as the following which reads the window title of each process and looks to see if the file name is contained within it:
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
string search = @"C:\test.doc";
Process[] procs = Process.GetProcesses();
foreach(Process proc in procs)
{
if (proc.MainWindowTitle.IndexOf(search) > - 1)
{
Console.WriteLine("{0} opened by {1}", search, proc.ProcessName);
}
}
Console.ReadLine();
}
}
However, in practice, I think you'd need to search for not only 'c:\test.doc' but also 'test.doc' and 'test' because some applications don't show the full path name. The problem, of course, is that this could therefore throw up a test.doc in a different folder or test.txt perhaps.