using System; using System.Management; namespace ZZ.Wmi { public class Win32ServiceManager { private string strPath; private ManagementClass managementClass; public Win32ServiceManager():this(".",null,null) { } public 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; } } // 驗證是否能串連到遠端電腦 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 object GetServiceValue(string serviceName,string propertyName) { ManagementObject mo = this.managementClass.CreateInstance(); mo.Path = new ManagementPath( this.strPath+".Name=/""+serviceName+"/""); return mo[propertyName]; |