0

I need to get the full path of a file which is opened in an application, by using the process.

For example, I have opened a txt file from C:\Users\Admin\Documents\sample.txt in a notepad. Whereas I have two notepad's being opened in desktop. If I used Process.GetProcessesByName("notepad"); I may get all the running instance of notepad and I can even get full path's of running process using Mainmodule.Filename

But my requirement is that I need to get the path of a file (i.e. sample.txt) from the process. Is it possible?

So for I have tried:

Process[] localByName = Process.GetProcessesByName("EA");
string path = localByName[0].Modules[0].FileName;

foreach (Process process in localByName)
{
}
sampathsris
  • 21,564
  • 12
  • 71
  • 98
arshad
  • 11
  • Imho this is not possible unless the application provides some API to do it. At most you will be able to get the command line arguments that were used to start the process - see [here](http://stackoverflow.com/questions/2633628/can-i-get-command-line-arguments-of-other-processes-from-net-c). Why exactly are you trying to do this? – vesan Sep 25 '14 at 04:32
  • Did you just post an empty code block and labelled it **so far I have tried?** – sampathsris Sep 25 '14 at 04:39
  • Please see [List of open files held by a process?](http://stackoverflow.com/q/2796083/1461424) and [How do I get the list of open file handles by process in C#?](http://stackoverflow.com/q/177146/1461424) – sampathsris Sep 25 '14 at 04:42
  • Some cases (when program keeps file open) covered in http://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c, otherwise it is not really possible in general case with information covered in jadavparesh06's answer. – Alexei Levenkov Sep 25 '14 at 05:50

1 Answers1

1

It all depends on whether the application that opens the text file holds a handle to the opened file or not. Notepad.exe for example opens the text file, copies its contents and releases the handle. You are left with the information provided by the window's title bar, which is not much.

If by contrast the file is opened by winword.exe, the application would hold a handle to the textfile. In that case you could use Mark Russinovich's handle.exe from the command line (or over Process.Start()) like this:

handle.exe test.txt

This would give you information about the full path of text.txt which your application could parse and use.

jadavparesh06
  • 926
  • 7
  • 11