检查文件是否已打开

时间:2012-06-16 12:13:39

标签: .net vb.net vb.net-2010

对于我的生活,我不明白为什么我的编码不起作用。如果文件在记事本中打开,以下编码会给我一条消息,但如果文件是以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

它似乎没有检查单词和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

1 个答案:

答案 0 :(得分:2)

因为p.ProcessName是“WINWORD” - > UpperCase
你测试“winword”, - >小写。

将测试更改为

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

忽略案例

相关问题