469,572 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

stopping window service through C#

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.
Mar 21 '07 #1
2 7524
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();
}

Vinki wrote:
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.
Mar 21 '07 #2
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.

"Karthik D V [C#,ASP.NET - MCAD]" wrote:
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();
}

Vinki wrote:
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.

Mar 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Daniel O'Brien | last post: by
2 posts views Thread by matteo | last post: by
5 posts views Thread by chris.hearson | last post: by
6 posts views Thread by D | last post: by
10 posts views Thread by archana | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.