3

I'm trying to retrieve the filename of a current process.

ie: If i have the file "test.txt" opened in Notepad, I need to get something like "c:\folder\test.txt"

The code below returns process informations, including the software path. (ie: "c:\windows\system32\notepad.exe").

[DllImport("user32.dll", EntryPoint = "GetForegroundWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetForegroundWindow();

[...]

public static string GetFilePathName()
{
    uint procId = 0;
    int hWnd = GetForegroundWindow();
    GetWindowThreadProcessId(hWnd, out procId);
    var proc = Process.GetProcessById((int)procId);
}

Is it possible to use this Process Class to achieve the opened filename/path the the current process is handling?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Andre Felipe
  • 169
  • 1
  • 7
  • I think you could do something like this: http://stackoverflow.com/questions/864484/getting-the-path-of-the-current-assembly – Nathan Tregillus May 07 '15 at 23:08
  • I belive you want command line arguments? http://stackoverflow.com/questions/504208/how-to-read-command-line-arguments-of-another-process-in-c – Arsen Mkrtchyan May 07 '15 at 23:08
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 07 '15 at 23:58

2 Answers2

1

In C#, I have the following solution. I wrote this to identify opened files in Notepad.exe to call Kill() on.

Your solution will depend on the window title, in this case I could expect formatting in Notepad to be "Untitled - Notepad", "config.ini - Notepad", "readme.txt - Notepad", etc. where I would split on the char '-' and trim the white space.

public static void getFileNameByProcess(string process)
{
    foreach (Process p in Process.GetProcessesByName(process))
    {
        string[] split = p.MainWindowTitle.Split('-');
        if (split.Length > 0)
            Console.WriteLine("\"" + split[0].Trim() + "\"");
    }
}

Use it like this: getFileNameByProcess("Notepad");

Example filenames:

  1. "config.ini"
  2. "readme.txt"
  3. "Untitled"

Using those file names, you could recursively search a directory if you had a general idea of where it is located however I would caution expensive calls like that

bkribbs
  • 734
  • 1
  • 6
  • 24
Tyler V
  • 272
  • 3
  • 11
0

It may be possible, but you'd have to really look at some interop to find out which particular file is open in Notepad.

You can get a lot of generic process information from .NET using the System.Diagnostics.Process class, which is pretty powerful.

https://msdn.microsoft.com/en-us/library/z3w4xdc9%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

For example to get all the notepad processes running on your machine.

Process[] notepads = Process.GetProcessesByName("notepad");

You may be able to get a library that does this too:

How do I get the list of open file handles by process in C#?

Community
  • 1
  • 1
garryp
  • 5,508
  • 1
  • 29
  • 41
  • 1
    Note that many programs that "open" file don't keep lock on it - and I believe notepad does not lock the file but rather simple reads it... – Alexei Levenkov May 07 '15 at 23:17
  • @Alexei is correct, when you load a file into Notepad, it reads the entire file into memory and closes the file, it does not keep an open handle on the file. – David Ching May 08 '15 at 02:57