【发布时间】:2010-01-08 17:43:54
【问题描述】:
我有一个想要在后台运行的应用程序。我想获取可执行文件的名称,例如 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。我想获取可执行文件名称(可能还有更多信息)。
我如何做到这一点?
【问题讨论】:
-
在stackoverflow上看这个问题的最后一个答案:stackoverflow.com/questions/7268302/…