我试图通过Handle获得Window的可执行路径 我使用以下代码来实现:
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out uint processId);
public string GetFilePath(IntPtr hwnd)
{
try
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid); //Gets the process by ID.
return proc.MainModule.FileName.ToString(); //Returns the path.
}
catch (Exception ex)
{
return ex.ToString();
}
}
它工作正常,除了一些应用程序(例如TeamSpeak 3 64bit [如果重要])。
如何以编程方式克服Access Denied问题?
我试图使用以下但同样的问题:
Get a executable path from a window handle?
Access denied while getting process path
How to Get Elevated Process Path in .NET