开发者

How to kill an instance of an application

开发者 https://www.devze.com 2023-03-20 10:41 出处:网络
I am having a text file in this path \"C:\\Test\\test.txt\" when this was openeed I need to close this.

I am having a text file in this path "C:\Test\test.txt" when this was openeed I need to close this.

When I am trying to use the below code all the instances of notepad are closing and I don't want that to be happened and I want to close only the ".txt" file:

Any help would开发者_Python百科 be appreciated!

Here is my code:

 Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
  For Each p As Process In Process
    p.Kill()
Next 


You can look at the

Process.MainWindowTitle

property of p.

Notepad's title will be Filename.txt - Notepad

If you started the process yourself, you can kill it using the Process.Kill() method.

Note that in many (most?) circumstances, killing all instances of a process isn't really a good user experience since the user may have started instances of that process on their own in addition to the instance your program launched / is attempting to close.


You could do something as mentioned about using an if statement. Assuming you opened the file called test.

 Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")      
    For Each p As Process In Process
        If p.MainWindowTitle.Contains("test") Then
            p.Kill()
        End If
    Next

EDIT:

To check for multiple files

simply add or to the .Contains line

If p.MainWindowTitle.Contains("test") Or ("blahblah") Then
p.kill()
0

精彩评论

暂无评论...
验证码 换一张
取 消