How to kill proccess by location

ankersya

New member
Joined
Jun 28, 2012
Messages
3
Programming Experience
Beginner
Okay, So i check if theres any .exe files in a folder and if there is i want to kill them, Here's the part were i try to kill any process(s)

Dim srp = sFile.Replace(".exe", "")
Dim process1 As Process
For Each process1 In Process.GetProcesses
If (process1.ProcessName = srp) Then
Dim killa As Process = process1
killa.Kill()
End If
Next


I don't get any error's but it doesn't seem to work, any help would be appreciated and don't laugh it's my first day :p
 
Wouldn't it make more sense to call GetProcessesByName rather than calling GetProcesses and then checking the name? Obviously you need to make sure that the name you specify is actually the name of the process.
 
I note that you don't have an object type definition in your loop which is why it doesn't work. So, in the economy version, ...

Dim srp = sFile.Replace(".exe", "")
For Each proc As Process In Process.GetProcessesByName(srp)
proc.Kill()
Next
 
The Process class has a property .MainModule of type ProcessModule, which in turn has a .FileName property you can use to figure out where the process was started from on disk. So...

Dim srp = sFile.Replace(".exe", "")
For Each proc As Process In Process.GetProcessesByName(srp)
    Dim strProcessPath As String = proc.MainModule.FileName
    If strProcessPath.SubString(0, strProcessPath.LastIndexOf("\") - 1).ToUpper = "C:\THISISABADPATH" Then
        proc.Kill()
    End If
Next


... will let you kill all instances of process with name srp (very bad variable naming BTW...) that started in C:\THISISABADPATH. All other instances will stay alive.
 
Ahh i get it, thanks heaps for the replys! I understand now that i need to use getprocessbyname

For Each sFile As String In System.IO.Directory.GetFiles(Environment.ExpandEnvironmentVariables("%UserProfile%\Downloads"), "*.exe")

Dim srp = sFile.Replace(".exe", "")

sFile outputs like "c:\users\user\Downloads\test" how do i remove "%UserProfile%\Downloads" from the line so getprocessbyname and kill will actually work? i tried sFile.Replace("%UserProfile%\Downloads", "") but that didn't work. Sorry for the noob questions lol
 
If all you want is the file name with no folder path and no extension then you should use the IO.Path.GetFileNameWithoutExtension method.

I tried

VB.NET:
For Each sFile As String In System.IO.Path.GetFileNameWithoutExtension(Environment.ExpandEnvironmentVariables("%UserProfile%\Downloads"))

and it didn't work, this is what i was trying before but also didn't work

VB.NET:
        For Each sFile As String In System.IO.Directory.GetFiles(Environment.ExpandEnvironmentVariables("%UserProfile%\Downloads"), "*.exe")
            For Each proc As Process In Process.GetProcessesByName(sFile)
              proc.Kill()
            Next
        Next
 
Um, how exactly are you going to get a file name from a folder path? If you want to get a file name you have to get it from a file path. Where is the file path in your code?
 
Back
Top