I am working on a C# Windows program that needs to kill a known
process. I have written code that works in development mode but hits
an "Access Denied" exception when running the compiled program:
Here's my original code:
// Now stop the server process
Process[] myProcesses;
myProcesses = Process.GetProcessesByName("MyServer");
foreach(Process myProcess in myProcesses)
{
myProcess.Kill();
}
It was suggested that I should use WMI, so I scrapped the above code
and wrote the following code:
ManagementScope ms = new ManagementScope ( @"root\cimv2" ) ;
string LSProcessName = "'MyServer'";
string querystring = "Select * from Win32_Process Where Name = " +
LSProcessName ;
ObjectQuery oq = new ObjectQuery ( querystring ) ;
ManagementObjectSearcher query = new ManagementObjectSearcher ( ms, oq
) ;
ManagementObjectCollection querycollection = query.Get( ) ;
foreach ( ManagementObject mo in querycollection )
mo.InvokeMethod("Terminate",null);
This code doesn't stop the process -- as the query doesn't find the
process. Any ideas what I may have wrong here?
Any/all help much appreciated. :?:
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com