如何确定C#中当前关注的进程名称和版本

时间:2020-03-06 14:34:04  来源:igfitidea点击:

例如,如果我正在使用Visual Studio 2008,则需要值为devenv和2008或者9.

版本号非常重要...

解决方案

该项目演示了我们需要的两个功能:EnumWindows和GetWindowtext

你能澄清你的问题吗?我们是说要运行一个程序,该程序会在活动窗口中告诉我们有关该程序的数据吗?还是我们想让程序报告自己的版本?

我们想要通过两种方式获取信息的方法都是System.Reflection.Assembly。 (请参阅链接中的代码示例。)

如何从外部程序获取程序集?我不确定那个...

这将是PInvoke城市...

我们需要在User32.dll中P调用以下API

Win32 :: GetForegroundWindow()返回当前活动窗口的HWND。

/// <summary>
/// The GetForegroundWindow function returns a handle to the foreground window.
/// </summary>
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

Win32 :: GetWindowThreadProcessId(HWND,LPDWORD)返回给定HWND的PID

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

在C#中

Process.GetProcessByID()使用PID创建一个Cprocess对象

processInstance.MainModule返回带有FileVersionInfo的ProcessModule。