Mercurial > hg > ucis.core
comparison Windows/ServiceManager.cs @ 0:3ab940a0c7a0
Initial commit
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 11 Sep 2012 16:28:53 +0200 |
parents | |
children | 4b78cc5f116b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ab940a0c7a0 |
---|---|
1 using System; | |
2 using System.Management; | |
3 using System.IO; | |
4 | |
5 namespace UCIS.Windows { | |
6 public class ServiceManager { | |
7 private ManagementObject _serviceObject; | |
8 | |
9 public const string StartMode_Automatic = "Automatic"; | |
10 public const string StartMode_Manual = "Manual"; | |
11 public const string StartMode_Disabled = "Disabled"; | |
12 | |
13 private ServiceManager(ManagementObject ob) { | |
14 _serviceObject = ob; | |
15 } | |
16 | |
17 public static ServiceManager GetServiceByPath(string FileName) { | |
18 ManagementClass mc = new ManagementClass("Win32_Service"); | |
19 foreach (ManagementObject ob in mc.GetInstances()) { | |
20 string Value = (string)ob.GetPropertyValue("PathName"); | |
21 if (Value == null) continue; | |
22 int Position = Value.IndexOf(FileName); | |
23 if (Position == 0 || Position == 1) { //Either <filepath> or <"filepath"> | |
24 return new ServiceManager(ob); | |
25 } | |
26 } | |
27 return null; | |
28 } | |
29 public static ServiceManager GetServiceByName(string Name) { | |
30 ManagementClass mc = new ManagementClass("Win32_Service"); | |
31 foreach (ManagementObject ob in mc.GetInstances()) { | |
32 string Value = (string)ob.GetPropertyValue("Name"); | |
33 if (Value == null) continue; | |
34 if (Value.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) return new ServiceManager(ob); | |
35 } | |
36 return null; | |
37 } | |
38 public static ServiceManager Create(string Name, string DisplayName, string PathName, string StartMode, bool DesktopInteract, string StartName, string StartPassword) { | |
39 ManagementClass mc = new ManagementClass("Win32_Service"); | |
40 UInt32 ret; | |
41 ret = (UInt32)mc.InvokeMethod("Create", new Object[] { | |
42 Name, //Name | |
43 DisplayName, //DisplayName | |
44 PathName, //PathName | |
45 16, //ServiceType (16 = own process) | |
46 1, //ErrorControl (1 = user is notified) | |
47 StartMode, //StartMode | |
48 DesktopInteract, //DesktopInteract | |
49 StartName, //StartName (null = LocalSystem) | |
50 StartPassword, //StartPassword | |
51 null, //LoadOrderGroup | |
52 null, //LoadOrderGroupDependencies | |
53 null //ServiceDependencies | |
54 }); | |
55 if (ret != 0) throw new ManagementException("Could not create service (code " + ret.ToString() + ")"); | |
56 return GetServiceByName(Name); | |
57 } | |
58 | |
59 public string Name { get { return (string)_serviceObject.GetPropertyValue("Name"); } } | |
60 public string DisplayName { get { return (string)_serviceObject.GetPropertyValue("DisplayName"); } } | |
61 public string PathName { get { return (string)_serviceObject.GetPropertyValue("PathName"); } } | |
62 public string StartMode { | |
63 get { return (string)_serviceObject.GetPropertyValue("StartMode"); } | |
64 set { _serviceObject.InvokeMethod("ChangeStartMode", new Object[] { value }); Refresh(); } | |
65 } | |
66 public bool DesktopInteract { get { return (bool)_serviceObject.GetPropertyValue("DesktopInteract"); } } | |
67 public string StartName { get { return (string)_serviceObject.GetPropertyValue("StartName"); } } | |
68 public string StartPassword { get { return (string)_serviceObject.GetPropertyValue("StartPassword"); } } | |
69 | |
70 public void Change(string DisplayName, string PathName, string StartMode, bool DesktopInteract, string StartName, string StartPassword) { | |
71 UInt32 ret; | |
72 ret = (UInt32)_serviceObject.InvokeMethod("Change", new Object[] { | |
73 DisplayName, //DisplayName | |
74 PathName, //PathName | |
75 16, //ServiceType (16 = own process) | |
76 1, //ErrorControl (1 = user is notified) | |
77 StartMode, //StartMode | |
78 DesktopInteract, //DesktopInteract | |
79 StartName, //StartName (null = LocalSystem) | |
80 StartPassword, //StartPassword | |
81 null, //LoadOrderGroup | |
82 null, //LoadOrderGroupDependencies | |
83 null //ServiceDependencies | |
84 }); | |
85 if (ret != 0) throw new ManagementException("Could not change service (code " + ret.ToString() + ")"); | |
86 Refresh(); | |
87 } | |
88 | |
89 public void Refresh() { | |
90 _serviceObject.Get(); | |
91 } | |
92 | |
93 public void Start() { | |
94 _serviceObject.InvokeMethod("StartService", null); | |
95 Refresh(); | |
96 } | |
97 public void Stop() { | |
98 _serviceObject.InvokeMethod("StopService", null); | |
99 Refresh(); | |
100 } | |
101 | |
102 public bool Running { | |
103 get { | |
104 Refresh(); | |
105 return (bool)_serviceObject.GetPropertyValue("Started"); | |
106 } | |
107 } | |
108 | |
109 public String State { | |
110 get { | |
111 Refresh(); | |
112 return (String)_serviceObject.GetPropertyValue("State"); | |
113 } | |
114 } | |
115 } | |
116 } |