2012-01-30 47 views

回答

1

您可以使用System.ServiceProcess.ServiceController(得到了这个answer的想法),通过启动或停止下列服务:

-Windows进程激活服务(WAS)
- 世界Wide Web Publishing服务( W3SVC)

下面是一些代码,会做的工作:

//stop iis (like running "IISReset /Stop") 
ResetService("WAS", false); 
ResetService("W3SVC", false); 

//start iis (like running "IISReset /Start") 
ResetService("WAS", true); 
ResetService("W3SVC", true); 

private static void ResetService(string name, bool start) 
{ 
    using (var service = new System.ServiceProcess.ServiceController(name)) 
    { 
     if (start && service.Status == ServiceControllerStatus.Stopped) 
     { 
      service.Start(); 
     } 
     else if (!start && service.Status == ServiceControllerStatus.Running) 
     { 
      service.Stop(); 
     } 
    } 
} 

至于其他IISRESET命令,你可以在超时代码很轻松了。要重新启动计算机,请检查this answer。让我知道你是否需要更多细节。

但是如果这对于一个派对来说是不够的,你可以在c#中使用this technique(如果你只需要完成这个工作,可以使用漂亮的平衡器)执行Power Shell脚本。

相关问题