简体   繁体   中英

How can I determine the current focused process name and version in C#

For example if I'm working on Visual Studio 2008, I want the values devenv and 2008 or 9.

The version number is very important...

This is going to be PInvoke city...

You'll need to PInvoke the following API's in User32.dll

Win32::GetForegroundWindow() in returns the HWND of the currently active window.

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

Win32::GetWindowThreadProcessId(HWND,LPDWORD) returns the PID of a given HWND

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

In C#

Process.GetProcessByID() takes the PID to create a C# process object

processInstance.MainModule returns a ProcessModule with FileVersionInfo attached.

项目演示了您需要的两个功能: EnumWindowsGetWindowtext

Can you clarify your question? Do you mean you want a program running, which will tell you data about the program in the active window? Or that you want your program to report out its own version?

What you're looking for to get the information either way is System.Reflection.Assembly . (See code examples in the link.)

How to get the assembly from an external program? That one I'm not sure about...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM