2

我想控制(启动和停止)远程机器中的 Windows 服务。我可以连接那台机器,但我无法访问 Windows 服务。这是我的代码。你能帮助我吗?

    ConnectionOptions oConn = new ConnectionOptions();
    oConn.Username = "****";
    oConn.Password = "****";

    ManagementScope managementScope = new ManagementScope(@"\\***.***.***.***\root\CIMV2", oConn);
    managementScope.Connect();

  RelatedObjectQuery roq = new RelatedObjectQuery("Win32_Service.Name='KanAktarim'");
  ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(managementScope, roq);
  ManagementObjectCollection mbCollection = moSearcher.Get();
4

1 回答 1

1
            ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
            moSearcher.Scope = managementScope;
            moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='KanAktarim'");
            ManagementObjectCollection mbCollection = moSearcher.Get();

            foreach (ManagementObject oReturn in mbCollection)
            {
                ManagementBaseObject outParams = oReturn.InvokeMethod("StartService", null, null);
                ManagementBaseObject outParams = oReturn.InvokeMethod("StopService", null, null);
                string a = outParams["ReturnValue"].ToString();

                string state = oReturn.Properties["State"].Value.ToString().Trim();
            }
于 2011-08-19T10:46:19.073 回答