Hi everyone,
Does any one know how to find out what files a process has open? for example if I have microsoft word open and it has a .doc file open, can I find what file is open by evaluating the Process object in c#? if not is there a better way to find out this info?
Basically I want to write an app that will show what windows programs i have open and allow me to close it, I also want to be able to reopen it from within my app and have the program open up the file(s) that it had open when i closed it.
Can anyone help with this?
thanks
Loading
AlanPosted Aug 29, 2007, 6:59 AM
You can check how many instances of a particular program are running and what files are open by parsing the Process.MainWindowTitle property. For example, in the case of Notepad:
Process[] procs = Process.GetProcessesByName("notepad");
string fileName = "";
foreach(Process proc in procs)
{
fileName = proc.MainWindowTitle.Trim().Replace(" - Notepad", "");
Console.WriteLine(fileName);
}
It's possible to get a list of all running processes on the local machine using the Process.GetProcesses() method.
Having obtained the Process object, you can close it gracefully by calling its CloseMainWindow() method if it has a user interface or close it abruptly by calling its Kill() method if it has no UI. Needless to say, the latter can be dangerous and should only be used where you know it's safe to terminate the process in this way.