473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to stop a process on local machine

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
Nov 15 '05 #1
0 1274

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John | last post by:
I am trying to spawn a seperate process from my asp page so when a user clicks the link or button a desktop program is launched. This will be on an intranet and the program will exist on the...
5
by: toby | last post by:
Can any one help? I'm trying to use System.Diagnostics.Process.Start to open legacy MS access aplications (ade and mdb files) and vb 6 .exe's using asp.net. There is something confusing going on?!?...
12
by: Vadym Stetsyak | last post by:
Hi there! Is there any way how can I stop garbage collection for a period of time. Lets say, I have a form. Before form load I pause the GC and when the form is shown resume GC
5
by: hangten | last post by:
I have a service that 1) uses filewatcher to watch a directory on a remote machine. 2) checks if a certain application is running on the local machine before starting it. The problem is while...
7
by: Denis Brkljacic | last post by:
Hi, I have made some simple ASP.NET (C#) application, that somewhere uses this command: Process aProcesses = Process.GetProcesses(Environment.MachineName); This command purpose is to find...
1
by: Indepth | last post by:
Overview: We're creating a ASP.NET app in C# that calls some C# programs we've created. Our DB server is on a w2k3 machine and using our w2k machines as our web server. Without setting an Identity...
0
by: martin | last post by:
Hi, I have a website on my local development machine that has a page where a small number of services (that I have wrote myself) can be either started or stopped. The website uses forms...
11
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then the process starts as the specified user, but hangs in a suspended state. In fact, any...
1
by: Daniel | last post by:
what permissions does a windows service need to execute another process? System.Diagnostics.Process process = System.Diagnostics.Process.Start(info); just local administrator? any specific...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.