stopping window service through C#

G

Guest

Hello everyone,

I have this application that stops and starts IIS admin. When I try to
stop the service. I get an error "Cannot open IISADMIN service on computer
'.'.". I tried changing the machine name to my machine name then also it does
not work.


Below is the code
ServiceController controller = new ServiceController();
//controller.MachineName = "CS1239876";
controller.MachineName=".";
controller.ServiceName = "IISADMIN";
string status = controller.Status.ToString();
if(status == "Running)
controller.Stop();

Can anyone please help me with this.

Any help will be appreciated.
 
K

Karthik D V [C#,ASP.NET - MCAD]

To run the IISADMIN service, you should have admin role in your
system.

The following code is a working code... i tried for Messanger since i
dont have "IISADMIN". I hope you will get ti done if you replace the
service name.

ServiceController controller = new ServiceController();
controller.ServiceName = "Messenger";
label1.Text = controller.Status.ToString();
if (controller.Status == ServiceControllerStatus.Running)
{
controller.Stop();
}
if (controller.Status == ServiceControllerStatus.Stopped)
{
controller.Start();
}

try this....OR try the below
ServiceController controller = new ServiceController("Messenger",
".");
if (controller.Status == ServiceControllerStatus.Running)
{
controller.Stop();
}
if (controller.Status == ServiceControllerStatus.Stopped)
{
controller.Start();
}
 
G

Guest

Hello Karthik,

I tried this code below

try
{
// ServiceController controller = new ServiceController();
// //controller.MachineName = "CSCRCAM848327";
// controller.MachineName=".";
// controller.ServiceName = "PDSSvc";
// string status = controller.Status.ToString();
// if(status == "Running")
// controller.Stop();
////////////////////////////////

ServiceController controller = new ServiceController("Messenger", ".");
if (controller.Status == ServiceControllerStatus.Running)
{
controller.Stop();
}
if (controller.Status == ServiceControllerStatus.Stopped)
{
controller.Start();
}




}
catch(Exception ex)
{
string x = ex.Message;
flag=true;
}

It didn't work and I am getting the same error. I have admin rights on my
computer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top