how to get the window title of a process using vb.net

后端 未结 2 498
萌比男神i
萌比男神i 2021-01-24 15:09

I am looking to go from the process name to the windows title.

e.g

\'winamp.exe\' -> \'1. Britney Spears - Hit me baby one more time\'

Thanks

Sol

相关标签:
2条回答
  • 2021-01-24 15:56

    Have a look at the System.Diagnostics.Process class:

    Process p = <Get process>
    Console.WriteLine(p.MainWindowTitle)
    
    0 讨论(0)
  • 2021-01-24 16:01

    Here's how to get the Filename as displayed in the Main Title Bar. This is the filename without the extension. Let's say that my Excel Workbook was named "ThisWorks.xlsx" This code returns "ThisWorks" without the file extension.

    Function OpenAppFileNames(ByVal exeName As String) As String
    
        Dim p() As Process = System.Diagnostics.Process.GetProcessesByName(exeName)
        Dim i As Integer = 0
        Dim x As Integer = UBound(p) + 1
        Dim Index As Long = 0
        Dim Title As String = ""
        Dim Result As String = ""
        Debug.Print(x)
        Do
            If i = x Then
                Exit Do
            End If
            Title = p(i).MainWindowTitle
            Result += vbNewLine & "Index " & Index + i & " = " & Title
            i = i + 1
            'MsgBox(Result)
        Loop Until i = x
    
        Return Result
    
    End Function
    

    p returns an array of integers starting at 0 (zero). If you know which order the processes are in, you can return the Title/Filename of the desired target.

    Hope this helps... took me 3 weeks to find this answer.

    0 讨论(0)
提交回复
热议问题