tags:

views:

537

answers:

4

Hi, 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.

A: 

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
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
@DrFuture if you don't have the pid, how do you choose the process to kill?
JaredPar
@JaredPar by username who started / owns the process
DrFuture
@DrFuture, you want to kill all processes by that particular user?
JaredPar
@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
A: 

edg answerd my Question too but i cant see the answer in the threadview??? Only at my Profile.

He wrote i should try to add the domain to the logindata. Its a local Account so i changed the code to

Shell("C:\WINDOWS\system32\taskkill.exe /S localhost /U " & System.Net.Dns.GetHostName() & "\userx .........

but nothing changed.

DrFuture
Maybe it's better to use System.Diagnostics.Process.Start(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "taskkill.exe"), String.Format("/S localhost /U {0}
abatishchev
+1  A: 
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
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
thx with administrativ rights its a true code.
DrFuture
A: 

http://stackoverflow.com/questions/562350/requested-registry-access-is-not-allowed here you can find the way to get required administrative rights (so called rights elevating)

abatishchev