我有一个应用程序,我想在后台运行。我想获取可执行文件的名称,例如IExplorer.exe。我玩过以下代码:C#获取有关当前活动窗口的信息
[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。我想获得可执行文件的名称(也许更多的信息)。
我该如何做到这一点?
看这个问题的计算器上的最后一个答案: http://stackoverflow.com/questions/7268302/get-the-titles-of-all-open-windows/31517889#31517889 – Godvicien 2015-07-20 13:44:30