.net下编程远程启动服务

来源:互联网 发布:matlab数据分析方法 编辑:程序博客网 时间:2024/05/22 00:36

 *********************************************************************************  
  using   System;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Data;  
  using   System.Drawing;  
  using   System.Web;  
  using   System.Web.SessionState;  
  using   System.Web.UI;  
  using   System.Web.UI.WebControls;  
  using   System.Web.UI.HtmlControls;  
  using   System.ServiceProcess;  
  using   System.Management;  
   
  namespace   ControlServer  
  {  
  ///   <summary>  
  ///   WebForm1   的摘要说明。  
  ///   </summary>  
      public   class   WebForm1   :   System.Web.UI.Page  
      {  
          protected   System.Web.UI.WebControls.Button   Button1;  
          private   string   strPath;    
          private   ManagementClass   managementClass;    
   
          private   void   Page_Load(object   sender,   System.EventArgs   e)  
            {  
  //   在此处放置用户代码以初始化页面  
            }  
   
        #region   Web   窗体设计器生成的代码  
        override   protected   void   OnInit(EventArgs   e)  
          {  
  //  
  //   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。  
  //  
              InitializeComponent();  
              base.OnInit(e);  
          }  
   
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {          
  this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);  
  this.Load   +=   new   System.EventHandler(this.Page_Load);  
   
  }  
  #endregion  
   
        private   void   Button1_Click(object   sender,   System.EventArgs   e)  
          {  
              if(   RemoteConnectValidate("****,****,****,****","***","***")   )  
                {  
  Win32ServiceManager("****,****,****,****","***","***");  
                                                          //         IP,                             用户名   密码  
  string   [,]   services   =   GetServiceList();  
  for(int   i=   0;i<services.GetLength(0);i++)  
        {  
              Response.Write(services[i,0]);  
              Response.Write(services[i,1]);  
              Response.Write(services[i,2]);  
              Response.Write(services[i,3]);  
              Response.Write("<br>");  
        }  
  string   strRst   =   null;    
  string   serviceName   =   "IISADMIN";  
  ManagementObject   mo   =   this.managementClass.CreateInstance();    
  mo.Path   =   new   ManagementPath(this.strPath+".Name=/""+serviceName+"/"");    
  mo.InvokeMethod("StartService",null);    
  //StartService   启动服务,PauseService   暂停服务,ResumeService   恢复服务,StopService   停止服务  
            }  
   
      }  
   
   
      //   验证是否能连接到远程计算机    
      public   static   bool   RemoteConnectValidate(string   host,string   userName,string   password)    
        {    
          ConnectionOptions   connectionOptions   =   new   ConnectionOptions();    
          connectionOptions.Username   =   userName;    
          connectionOptions.Password   =   password;    
          ManagementScope   managementScope   =   new   ManagementScope(   "////"   +host+   "//root//cimv2",connectionOptions)   ;    
          try    
              {    
  managementScope.Connect();    
              }    
          catch    
              {    
              }    
              return   managementScope.IsConnected;    
          }    
      //   获取所连接的计算机的所有服务数据    
        public   string   [,]   GetServiceList()    
          {    
            string   [,]   services   =   new   string   [this.managementClass.GetInstances().Count,4];    
            int   i   =   0;    
            foreach(ManagementObject   mo   in   this.managementClass.GetInstances())    
              {    
                    services[i,0]   =   (string)mo["Name"];    
                    services[i,1]   =   (string)mo["DisplayName"];    
  services[i,2]   =   (string)mo["State"];    
  services[i,3]   =   (string)mo["StartMode"];    
  i++;    
              }    
  return   services;    
  }    
   
   
    public   void   Win32ServiceManager(string   host,string   userName,string   password)    
        {    
            this.strPath   =   "////"+host+"//root//cimv2:Win32_Service";    
            this.managementClass   =   new   ManagementClass(strPath);    
            if(userName!=null&&userName.Length>0)    
  {    
  ConnectionOptions   connectionOptions   =   new   ConnectionOptions();    
  connectionOptions.Username   =   userName;    
  connectionOptions.Password   =   password;    
  ManagementScope   managementScope   =   new   ManagementScope(   "////"   +host+   "//root//cimv2",connectionOptions)   ;    
  this.managementClass.Scope   =   managementScope;    
  }    
   
          }  
      }  
  }   
 

原创粉丝点击