我正在尝试检索当前进程的文件名。
即:如果我在记事本中打开了文件“test.txt”,我需要得到类似“c:\folder\test.txt”的东西
下面的代码返回进程信息,包括软件路径。(即:“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);
}
是否可以使用此进程类来实现当前进程正在处理的打开文件名/路径?