通过句柄获取窗口的可执行路径 – 拒绝访问

我试图通过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问题?

我试图使用以下但同样的问题:
从窗口句柄获取可执行文件路径?
获取进程路径时访问被拒绝
如何在.NET中获得提升的进程路径