Hi all,
I have a file, named "test.cs". it will be opened by many editors(like notepad, MSWord, Word Pad, VS IDE etc).
I want to write a functin in my application that detect the "test.cs" file is opened by which editor or process?
I needs this process-ID ?
thanks
How to check the the file is opened or not?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Mar 26, 2008, 4:23 AM EDT
Realistically, I think the only way you're going to be able to do this is to search the main window title of each running process to see whether it contains the word 'test' or not and, if it does, to obtain its Id.
As you know, extensions are not always shown in the title bar for some applications such as notepad and wordpad though I think you'll be OK with test.cs. If you're confident that there will be no clashes with other similarly named files such as test.txt being open at the same time, you could leave out the extension if you wish.
Here's some basic code:
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string fileName = "test.cs"; // though extensions are sometimes ignored
RegexOptions ro = RegexOptions.IgnoreCase | RegexOptions.Compiled;
Regex r = new Regex(@"\b" + fileName + @"\b", ro);
Process[] procs = Process.GetProcesses();
Console.WriteLine("{0} has been opened by the following processes :\n",fileName);
foreach(Process proc in procs)
{
if (r.IsMatch(proc.MainWindowTitle))
{
Console.WriteLine(" {0,-10} Id {1}", proc.ProcessName, proc.Id);
}
}
}
}