问题
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...
回答1:
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.
回答2:
This project demonstrates the two functions you need: EnumWindows and GetWindowtext
回答3:
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...
来源:https://stackoverflow.com/questions/117851/how-can-i-determine-the-current-focused-process-name-and-version-in-c-sharp