2011-03-11 69 views

回答

0

这不是一个好方法IMO。 Web应用程序应该在浏览器中进行沙盒处理,不应以任何方式访问系统资源。您可能想要创建一个WCF Web服务,它将由Windows服务调用,然后您的Web应用程序可以访问Web服务。

为了回答这个问题的缘故:

Original Link for Code

private void Page_Load(object sender, System.EventArgs e) 
     { 
string PcName = "PC Name"; 
      WindowsImpersonationContext wic = null; 

      try 
      { 

       IPrincipal p = this.User; 

       WindowsIdentity id = (WindowsIdentity)p.Identity; 

       Response.Write("Running as:" + WindowsIdentity.GetCurrent().Name + "<br>"); 

       wic = id.Impersonate(); 

       Response.Write("Running as:" + WindowsIdentity.GetCurrent().Name + "<br>"); 

       ServiceController[] services = ServiceController.GetServices(); 

       for(int i = 0; i < services.Length; i++) 
       { 

        if (services[i].DisplayName == "COM+ System Application") 
        { 
         Response.Write(services[i].DisplayName + " - "); 
         Response.Write(services[i].Status.ToString()); 
         Response.Write("<br>"); 
         ServiceController sc = new ServiceController(services[i].DisplayName,PcName); 

         if (sc.Status == ServiceControllerStatus.Running) 
         { 
          sc.Stop(); 
         } 
         if (sc.Status == ServiceControllerStatus.Stopped) 
         { 
          sc.Start(); 
         } 

        } 
       } 
      } 
      catch(Exception err) 
      { 
       Response.Write(err.Message); 
      }   
      finally 
      { 
       wic.Undo(); 
      } 

     } 

但是,你可能会遇到的安全问题,所以看看线程中的所有响应。