2

I try to kill all processes of a specified user.

i use

       Try
        Shell("C:\WINDOWS\system32\taskkill.exe /S localhost /U userx /P passwort /f /FI " & Chr(34) & "USERNAME eq userx" & Chr(34))
    Catch ex As Exception
        MessageBox.Show("LogoutException occurred. " + ex.Message)
    End Try

But Nothing happened. If i try to use this taskkill..... command by console it works fine. one of the apps that should be closed is the explorer.exe. All apps`s From the user must be closed.

I inserted /u /p because the application it self runs under a different user.

has anyone an idea how i could kill truely all proccesses from that 1 user?

EDIT: i forget a little information, The application is started by a user with user-rights. thats the reason i use taskkill - there i can enter a different user with administrativ privilegs. So the second Problem is that i can`t use process.kill directly.

thx a lot for help.

DrFuture
  • 53
  • 1
  • 6

2 Answers2

1
foreach (Process p in Process.GetProcesses())
{
    if (String.Equals(p.ProcessName, name))
    {
        p.Kill();
    }
}

or

Process.GetProcesses()
.Where(p => String.Equals(p.ProcessName, name))
.First()
.Kill(); // kills only first

or

Process.GetProcesses()
.Where(p => String.Equals(p.ProcessName, name))
.ToList()
.ForEach(p => p.Kill()); // kills all
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Thx a LOT! - i forget a important info that the application it self has`t any administrativ rights - so i could not kill applications from a other user. at my code i use an user with administrativ rights at the program-parameters of taskkill. - i has edit my entrypost. sorry... – DrFuture Apr 06 '09 at 13:56
0

If you know the ID of the process, you can kill it via the Process class. Take this code snippet for example

Public Sub KillProcess(id as Integer)
  For Each p as Process in Process.GetProcesses()
    if p.Id = id Then
      p.Kill()
    End If
  Next
End SUb
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thx, but i havent`t the pid`s. the process`s are different every time - only if i can readout all process-id`s from one user. Can I readout if a process is killed i like to try it more times to kill a app if the first try failed. – DrFuture Apr 06 '09 at 13:06
  • @DrFuture if you don't have the pid, how do you choose the process to kill? – JaredPar Apr 06 '09 at 13:08
  • @JaredPar by username who started / owns the process – DrFuture Apr 06 '09 at 13:12
  • @DrFuture, you want to kill all processes by that particular user? – JaredPar Apr 06 '09 at 13:14
  • @JaresPar Yes the app is started within user-context and a admin enter her user/pw. the programm should now kill with using of the adminaccount all processes that runs with the userx. So i have administrativ privilegs that should be able to kill all processes from any user i like. – DrFuture Apr 06 '09 at 13:20