我在下面使用了这段代码,并获得了在Window中运行的所有应用程序。
现在我只想获得.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;
}
答案 0 :(得分: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
方法会相应地返回true
或false
。