【问题标题】:how to get the window title of a process using vb.net如何使用 vb.net 获取进程的窗口标题
【发布时间】:2010-06-28 09:32:27
【问题描述】:

我希望从进程名称转到 Windows 标题。

例如

'winamp.exe' -> '1.布兰妮斯皮尔斯 - 再打我一次宝贝'

谢谢

解决方案:

Dim p As Process

For Each p In Process.GetProcessesByName("winamp")

    MsgBox(p.MainWindowTitle.ToString)

Next

【问题讨论】:

  • ToString 在您的代码中完全是多余的。 MainWindowTitle 已经是一个字符串!
  • 支持窗口标题。

标签: vb.net


【解决方案1】:

看看System.Diagnostics.Process 类:

Process p = <Get process>
Console.WriteLine(p.MainWindowTitle)

【讨论】:

  • 谢谢.. MainWindowTitle 正是我所需要的 :)
【解决方案2】:

以下是获取主标题栏中显示的文件名的方法。这是没有扩展名的文件名。假设我的 Excel 工作簿被命名为“ThisWorks.xlsx”此代码返回“ThisWorks”而没有文件扩展名。

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 返回一个从 0(零)开始的整数数组。如果您知道进程的顺序,则可以返回所需目标的标题/文件名。

希望这会有所帮助...我花了 3 周时间才找到这个答案。

【讨论】:

  • 需要注意的是,有些应用程序只打开一个实例,例如 MS Word 和 MS Excel,但该单个实例可能会打开多个工作簿。
猜你喜欢
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
相关资源
最近更新 更多