My user is in the Administrators group. I am using the .NET WMI API (System.Management) to kill a process using this code:
var scope = new ManagementScope("\root\cimv2");
scope.Connect();
var query = new ObjectQuery(
string.Format("Select * from Win32_Process Where ProcessID = {0}",
processId));
var searcher = new ManagementObjectSearcher(scope, query);
var coll = searcher.Get().GetEnumerator();
coll.MoveNext();
var mgmtObj = (ManagementObject)coll.Current;
var ret = (uint) mgmtObj.InvokeMethod("Terminate");
// ret == 2 here, meaning "Access Denied"
It's failing to kill the process and returning a 2
(Access Denied
). However if I use:
Process.Start("cmd", string.Format("/c \"taskkill /f /pid {0}\"", processId));
the process gets killed. (but if I leave out /f
it fails).
Is there any way to terminate the process using WMI?
EDIT: I found the following on http://msdn.microsoft.com/en-us/library/windows/desktop/aa393907(v=vs.85).aspx:
To terminate a process that you do not own, enable the SeDebugPrivilege privilege.
The page provides VBScript code but how would I do this in C#?