我有一个要在后台运行的应用程序。我想获取可执行文件名称,例如 IExplorer.exe。我玩过以下代码:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static void Main()
{
int chars = 256;
StringBuilder buff = new StringBuilder(chars);
while (true)
{
// Obtain the handle of the active window.
IntPtr handle = GetForegroundWindow();
// Update the controls.
if (GetWindowText(handle, buff, chars) > 0)
{
Console.WriteLine(buff.ToString());
Console.WriteLine(handle.ToString());
}
Thread.Sleep(1000);
}
}
这只会让我获得窗口标题和句柄 ID。我想获取可执行文件名称(可能还有更多信息)。
我该如何实现?
最佳答案
我认为您需要“GetWindowModuleFileName ()”而不是 GetWindowText 你传入 hwnd,所以你仍然需要调用 GetForegroundWindow()
关于C#获取当前事件窗口的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2029495/