我想控制(启动和停止)远程计算机中的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();

最佳答案

            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();
            }

关于c# - 如何使用Related ObjectQuery访问romote Windows服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7117877/

10-15 06:26