Click here to Skip to main content
15,672,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to kill Process by Original File Name?

In vb.net?


Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jul-11 1:27am    
What is "original name"?
--SA

Try
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName(sname)

For Each p As Process In pProcess
p.Kill()
Next
 
Share this answer
 
v2
Comments
amit_myana 23-Jul-11 5:45am    
I try the program. but the problem is I want to kill the process at run time by the original file name. when i used the above code it will kill by the image name.
Sergey Alexandrovich Kryukov 24-Jul-11 1:34am    
Abhinav, OP asks for a thing which is a bit different; I answered, please see.
--SA
First, get all processes using System.Diagnostics.Process.GetProcesses. For each process, get its main module using System.Diagnostics.Process.MainModule. For this module of each process, get its file name using System.Diagnostics.ProcessModule.FileName. When you have a match, kill matching process by System.Diagnostics.Process.Kill.

See:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule.aspx[^].

—SA
 
Share this answer
 
Comments
amit_myana 25-Jul-11 9:13am    
Hi Friend,
Thanks for reply

I want to doing application like exe blocker/restriction at rutime...
but when i run this code then it will increase size (task manager- I/O Read Bytes) plz tell me how to solve this...
Thanks in Advance...


Private Sub Pro_run_Time_Tick1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pro_run_Time1.Tick
Try

For i As Integer = 0 To DGV_Process_kill.RowCount - 1
Dim prc1() As Process
Dim machine_name As String = System.Environment.MachineName.ToString
prc1 = Process.GetProcesses(machine_name)
getnametouseforblocking = DGV_Process_kill.Rows(i).Cells(4).Value()
For abc As Integer = 0 To UBound(prc1)
Dim t_OrgFile As String = ""
Try

t_OrgFile = prc1(abc).MainModule.FileVersionInfo.OriginalFilename.ToString.ToUpper
Catch ex As Exception
End Try
'If getnametouseforblocking = "" And t_OrgFile = "" Then

'Else
If getnametouseforblocking = t_OrgFile Then
Try
prc1(abc).Kill()
MsBox(Me, "You have not permission to use..", "C", Me)
Catch ex As Exception
End Try
End If
'End If
Next
Next

Catch ex As InvalidOperationException
End Try

Sorry for my Bad English....
Sergey Alexandrovich Kryukov 25-Jul-11 9:49am    
It's very bad that you use timer (as I can understand). If you need some events from file system to handle changes, use FileSystemWatcher. This code does not show how it works as some parts of it are somewhere else. Why are you getting processes and killing some of them? Something you mean by exe blocker/restriction? Why?
--SA
Sergey Alexandrovich Kryukov 25-Jul-11 10:04am    
Why are you taking MainModule.FileVersionInfo.OriginalFileName not MainModule.FileName? Why you convert it ToString? It's already a string.
--SA
amit_myana 26-Jul-11 3:50am    
okThanks for reply,

can we watch .exe file is run by user using FileSystemWatcher1

Thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900