2011-03-11 71 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(); 
      } 

     } 

但是,你可能會遇到的安全問題,所以看看線程中的所有響應。