通过C#代码远程机器上启动和停止IIS [英] Start and Stop IIS on remote machine through C# code

查看:226
本文介绍了通过C#代码远程机器上启动和停止IIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要停止IIS在远程机器上,然后做一些工作,然后重新启动IIS服务,一旦工作完成。我试图做到这一点使用C#代码。我所看到的关于通过代码远程计算机上启动IIS一些类似的问题。但我一直无法从中得到任何工作的解决方案。一些皆伐C#代码有关如何执行启动和停止操作将是非常有益的。


解决方案

Microsoft.Web.Administration 命名空间有您所需要的管理IIS。瞧瞧吧:的http:// msdn.microsoft.com/en-us/library/microsoft.web.administration%28VS.90%29.aspx



不过,如果你只是想停止和启动服务,您可以使用管理服务 Service.Controller

  ServiceController的服务=新的ServiceController(服务名); 
超时时间跨度= TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running,超时);



Service.Controller 类页面都应该有你所需要的信息。在, http://msdn.microsoft.com/en搜寻-us /库/ system.serviceprocess.servicecontroller.aspx



下面是你可以用它来启动服务的完整例子:

 公共静态无效StartService(字符串服务名,诠释timeoutMilliseconds)
{
ServiceController的服务=新的ServiceController(服务名);

{
超时时间跨度= TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running,超时);
}

{
// ...
}
}


查看上面的链接了解更多的文档。



您可以连接到远程服务器,如下所示:

  ServiceController的SVC =新的ServiceController(服务,计算机); 

如果您需要在远程服务器上一组不同的权限有涉及更多的工作。看到这个问题,与ServiceController的和模拟启动远程的Windows服务


I need to stop IIS on a remote machine and then do some work and then start the IIS service again once the work is done. I am trying to do this using C# code. I have seen some similar questions about starting IIS on remote machines through code. But I have not been able to get any working solution from it. Some clearcut C# code about how to do the start and stop operations would be really helpful.

解决方案

The Microsoft.Web.Administration Namespace has what you need for administering IIS. Check it out at: http://msdn.microsoft.com/en-us/library/microsoft.web.administration%28VS.90%29.aspx.

However if you just want to stop and start services you can manage your services using the Service.Controller

 ServiceController service = new ServiceController(serviceName);
 TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
 service.Start();
 service.WaitForStatus(ServiceControllerStatus.Running, timeout);

The Service.Controller class page should have all the information you require. Find it at, http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx

Here's a complete example you can use to start a service:

public static void StartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}

Review the link above for more documentation.

You can connect to a remote server as follows:

ServiceController svc =  new ServiceController("Service", "COMPUTERNAME");

If you require a different set of permissions on the remote server there's a lot more work involved. See this question, Starting remote Windows services with ServiceController and impersonation.

这篇关于通过C#代码远程机器上启动和停止IIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆