2015-11-06 219 views
3

我需要知道如何讓應用程序名稱類似這樣的,當我有進程名稱:如何從進程名稱獲取應用程序名稱?

enter image description here

我迄今取得的進展:

Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpdwProcessId As Int32) As Int32 
Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow"() As IntPtr 
'Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As System.IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer 
Private makel As String 
Private Function GetActiveAppProcess() As Process 
    Dim activeProcessID As IntPtr 
    GetWindowThreadProcessId(GetForegroundWindow(), activeProcessID) 
    Return Process.GetProcessById(activeProcessID) 
End Function 
Sub GetProcInfo() 
    Dim activeProcess As Process = GetActiveAppProcess() 
    With activeProcess 
     ProcessName = .ProcessName 
     Windowtitle = .MainWindowTitle 
     'Application name = ? 
    End With 
End Sub 

我幾乎沒有不同之處在於Process.ProcessName返回「資源管理器」。我怎樣才能得到「Windows資源管理器」的價值? 任何幫助表示讚賞。

+2

使用Process.MainModule.Filename獲取exe的路徑。然後FileVersionInfo.GetVersionInfo(path).FileDescription來獲取描述。 –

+0

太棒了!它工作:)謝謝漢斯! ApplicationName = FileVersionInfo.GetVersionInfo(activeProcess.MainModule.FileName).FileDescription – user3397557

+0

@HansPassant您可以添加答案以解決此問題嗎? –

回答

1

由於Hans尚未發佈答案,因此他在問題的評論中建議的答案如下。我希望每個人都不介意我冒昧地繼續寫下答案。

Sub GetProcInfo() 
    Dim activeProcess As Process = GetActiveAppProcess() 
    With activeProcess 
     ApplicationName = .MainModule.FileVersionInfo.FileDescription 
     ProcessName = .ProcessName 
     WindowTitle = .MainWindowTitle 
    End With 
End Sub 
相關問題