.net4.0 c# 启动远程服务器windows服务

来源:互联网 发布:u盘怎么安装ubuntu系统 编辑:程序博客网 时间:2024/06/05 17:09
    private string strPath;      private ManagementClass managementClass;            public void startService()      {          if (remoteConnectValidate("192.168.1.100", "username", "password"))          {              win32ServiceManager("192.168.1.231", "username", "password");              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 serviceName = "IISADMIN";              ManagementObject mo = this.managementClass.CreateInstance();              mo.Path = new ManagementPath(this.strPath + ".Name=\"" + serviceName + "\"");              //StartService 启动服务,PauseService 暂停服务,ResumeService 恢复服务,StopService 停止服务              mo.InvokeMethod("StartService", null);          }      }      /// <summary>      /// 验证是否能连接到远程计算机        /// </summary>      /// <param name="host">主机名或ip</param>      /// <param name="userName">用户名</param>      /// <param name="password">密码</param>      /// <returns></returns>      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;      }      /// <summary>      /// 获取所连接的计算机的所有服务数据      /// </summary>      /// <returns></returns>      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;          }    }
微笑参考文献http://wenku.baidu.com/view/413f009b51e79b896802264e.html?from=related