.net 检查文件是否已打开
就我的一生而言,我不明白为什么我的编码不起作用。如果文件是在记事本中打开的,下面的编码确实会给我一条消息,但如果文件是在word或excel中打开的,则不会给我一条消息.net 检查文件是否已打开,.net,vb.net,vb.net-2010,.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.Conta
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
及
因为p.ProcessName是“WINWORD”->大写
您测试的是“winword”->小写 将您的测试更改为
if(String.Compare(p.ProcessName, "winword", true))
.....
要忽略大小写在第一个示例中,
MainWindowTitle
之后的.ToString
有什么问题?是否尝试调试?!我建议你这样做,那么你会很容易发现问题所在
if(String.Compare(p.ProcessName, "winword", true))
.....