C#获取当前事件窗口的信息

标签 c# process window

我有一个要在后台运行的应用程序。我想获取可执行文件名称,例如 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/

相关文章:

c - 如何将对话框置于主程序窗口位置的中心?

c# - 模拟 Autofac.IComponentContext 单元测试 c#

linux - 如何使用内核模块监控进程创建和统计

c++ - 无限循环中的 Windows 消息泵

c# - 当前进程的性能计数器 CPU 使用率超过 100

c# - 判断进程是否为系统进程

java - 如何以编程方式将按键事件发送到 Java 应用程序中的任何窗口/进程?

c# - ASP.NET MVC 3 中的更新面板

c# - 如何创建 IPortableDeviceManager 的实例?

C# 获取从 X 开始按名称排序的目录