1

我在下面使用了这段代码,我得到了在窗口中运行的所有应用程序。

现在我只想获取.Net (.exe) 的应用程序。你能帮助我吗?

public Form1()
{
    InitializeComponent();
    EnumWindows(new WindowEnumCallback(this.AddWnd), 0);
}

public delegate bool WindowEnumCallback(int hwnd, int lparam);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(WindowEnumCallback lpEnumFunc, int lParam);

[DllImport("user32.dll")]
public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);

[DllImport("user32.dll")]
public static extern bool IsWindowVisible(int h);

private List<string> Windows = new List<string>();
private bool AddWnd(int hwnd, int lparam)
{
    if (IsWindowVisible(hwnd))
    {
        StringBuilder sb = new StringBuilder(255);
        GetWindowText(hwnd, sb, sb.Capacity);
        Windows.Add(sb.ToString());
        txtTest.Text += hwnd.ToString()+" ";
        lst.Items.Add(sb.ToString());
    }

    return true;
}
4

1 回答 1

0

您可以使用如下方法以及 P/Invoke 函数GetFileVersion()

[DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
private static extern int GetFileVersion(string path, StringBuilder buffer, int buflen, out int written);

public static bool IsDotNetExecutable(string path)
{
    StringBuilder sBuilder = new StringBuilder(256);
    int written;
    int gvf = GetFileVersion(path, sBuilder, sBuilder.Capacity, out written);

    return gvf == 0;
}

该函数在失败时不会抛出异常。0 以外的返回值表示发生错误并且可执行文件不是 .Net 程序集。因此IsDotNetExecutable上面的方法返回truefalse相应地返回。

于 2013-04-15T10:05:22.273 回答