-2

对于我的生活,我看不出为什么我的编码不起作用。如果文件在记事本中打开,以下编码确实会给我一条消息,但如果文件在 word 或 excel 中打开,则不会?

 Dim apps = 0
    Dim Process() As Process = System.Diagnostics.Process.GetProcesses
    For Each p As Process In Process
        If p.MainWindowTitle.ToString.Contains("test") Then
            If p.ProcessName = "notepad" Then
                MsgBox("test file is open in notepad")
                apps += 1
            ElseIf p.ProcessName = "winword" Then
                MsgBox("test file is open in word")
                apps += 1
            ElseIf p.ProcessName = "excel" Then
                MsgBox("test file is open in excel")
                apps += 1
            End If
        End If
    Next

    If apps = 0 Then
        'run my code
    End If

它似乎没有检查 word 和 excel,但以下两个编码片段都有效?

   Dim Process2() As Process = System.Diagnostics.Process.GetProcessesByName("winword")
    For Each p As Process In Process2
        If p.MainWindowTitle.Contains("test") Then
            MsgBox("test file is open in word")
        End If
    Next

   Dim Process2() As Process = System.Diagnostics.Process.GetProcessesByName("excel")
    For Each p As Process In Process2
        If p.MainWindowTitle.Contains("test") Then
            MsgBox("test file is open in excel")
        End If
    Next
4

1 回答 1

2

因为 p.ProcessName 是“WINWORD”->
您测试“winword”的大写,->小写。

将您的测试更改为

if(String.Compare(p.ProcessName, "winword", true))
   .....

无视案件

于 2012-06-16T12:44:51.570 回答