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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Apr 29, 2008, 3:17 PM EDT
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.