Sie benötigen Windows-API verwenden:
Private Sub Main()
Dim shortName as String = GetShortEXEName(GetForegroundWindow())
End Sub
' Define other methods and classes here
<DllImport("user32.dll", SetLastError := True)> _
Public Shared Function GetForegroundWindow() As IntPtr
End Function
Public Shared Function GetProcess(hwnd As IntPtr) As Process
Dim intID As Integer = 0
GetWindowThreadProcessId(hwnd, intID)
Return Process.GetProcessById(intID)
End Function
<DllImport("user32.dll", SetLastError := True)> _
Public Shared Function GetWindowThreadProcessId(hwnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
End Function
Public Shared Function GetShortEXEName(hwnd As IntPtr) As String
' this seems to be better to use than calling GetProcessEXEName and then using substring functions to get the short executable name
' because that function seems to crash when dealing with Windows Explorer windows
Try
Return GetProcess(hwnd).ProcessName
Catch
Return ""
End Try
End Function