[转]提供一个C#编写的基类源码(用于操作WINDOWNS系统的服务)
http://bbs.pediy.com/showthread.php?t=77439&prefixid=phpforce_40
可以对系统服务 检查 增加 删除 启动 停止 重启
Code
1/**//// <summary>
2 /// Windows服务类
3 /// </summary>
4 public class ServiceUl
5 {
6 /**//// <summary>
7 /// 检查服务存在的存在性
8 /// </summary>
9 /// <param name=" NameService ">服务名</param>
10 /// <returns>存在返回 true,否则返回 false;</returns>
11 public static bool isServiceIsExisted(string NameService)
12 {
13 ServiceController[] services = ServiceController.GetServices();
14 foreach (ServiceController s in services)
15 if (s. NameService.ToLower() == NameService.ToLower())
16 return true;
17 return false;
18 }
19
20 /**//// <summary>
21 /// 安装Windows服务
22 /// </summary>
23 /// <param name="stateSaver">集合</param>
24 /// <param name="filepath">程序文件路径</param>
25 public static void InstallmyService(IDictionary stateSaver, string filepath)
26 {
27 try
28 {
29 AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
30 AssemblyInstaller1.UseNewContext = true;
31 AssemblyInstaller1.Path = filepath;
32 AssemblyInstaller1.Install(stateSaver);
33 AssemblyInstaller1.Commit(stateSaver);
34 AssemblyInstaller1.Dispose();
35 }
36 catch (Exception ex)
37 {
38 throw new SysException(ex.Message, ex);
39 }
40 }
41
42 /**//// <summary>
43 /// 卸载Windows服务
44 /// </summary>
45 /// <param name="filepath">程序文件路径</param>
46 public static void UnInstallmyService(string filepath)
47 {
48 try
49 {
50 AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
51 AssemblyInstaller1.UseNewContext = true;
52 AssemblyInstaller1.Path = filepath;
53 AssemblyInstaller1.Uninstall(null);
54 AssemblyInstaller1.Dispose();
55 }
56 catch (Exception ex)
57 {
58 throw new SysException(ex.Message, ex);
59 }
60 }
61
62 /**//// <summary>
63 /// 检查Windows服务是否在运行
64 /// </summary>
65 /// <param name="name">程序的服务名</param>
66 public static bool IsRunning(string name)
67 {
68 bool IsRun = false;
69 try
70 {
71 if (!ServiceIsExisted(name)) return false;
72 var sc = new ServiceController(name);
73 if (sc.Status == ServiceControllerStatus.StartPending || sc.Status == ServiceControllerStatus.Running)
74 {
75 IsRun = true;
76 }
77 sc.Close();
78 }
79 catch
80 {
81 IsRun = false;
82 }
83 return IsRun;
84 }
85
86 /**//// <summary>
87 /// 启动Windows服务
88 /// </summary>
89 /// <param name="name">程序的服务名</param>
90 /// <returns>启动成功返回 true,否则返回 false;</returns>
91 public static bool StarmyService(string name)
92 {
93 try
94 {
95 var sc = new ServiceController(name);
96 if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending)
97 {
98 sc.Start();
99 sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
100 }
101 else
102 {
103
104 }
105 sc.Close();
106 return true;
107 }
108 catch (Exception ex)
109 {
110 throw new SysException(ex.Message, ex);
111 }
112 }
113
114 /**//// <summary>
115 /// 停止Windows服务
116 /// </summary>
117 /// <param name="name">程序的服务名</param>
118 /// <returns>停止成功返回 true,否则返回 false;</returns>
119 public static bool StopmyService(string name)
120 {
121 try
122 {
123 var sc = new ServiceController(name);
124 if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending)
125 {
126 sc.Stop();
127 sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
128 }
129 else
130 {
131
132 }
133 sc.Close();
134 return true;
135 }
136 catch (Exception ex)
137 {
138 throw new SysException(ex.Message, ex);
139 }
140 }
141
142 /**//// <summary>
143 /// 重启Windows服务
144 /// </summary>
145 /// <param name="name">程序的服务名</param>
146 /// <returns>重启成功返回 true,否则返回 false;</returns>
147 public static bool RefreshmyService(string name)
148 {
149 return StopmyService(name) && StarmyService(name);
150 }
151 }
152
1/**//// <summary>
2 /// Windows服务类
3 /// </summary>
4 public class ServiceUl
5 {
6 /**//// <summary>
7 /// 检查服务存在的存在性
8 /// </summary>
9 /// <param name=" NameService ">服务名</param>
10 /// <returns>存在返回 true,否则返回 false;</returns>
11 public static bool isServiceIsExisted(string NameService)
12 {
13 ServiceController[] services = ServiceController.GetServices();
14 foreach (ServiceController s in services)
15 if (s. NameService.ToLower() == NameService.ToLower())
16 return true;
17 return false;
18 }
19
20 /**//// <summary>
21 /// 安装Windows服务
22 /// </summary>
23 /// <param name="stateSaver">集合</param>
24 /// <param name="filepath">程序文件路径</param>
25 public static void InstallmyService(IDictionary stateSaver, string filepath)
26 {
27 try
28 {
29 AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
30 AssemblyInstaller1.UseNewContext = true;
31 AssemblyInstaller1.Path = filepath;
32 AssemblyInstaller1.Install(stateSaver);
33 AssemblyInstaller1.Commit(stateSaver);
34 AssemblyInstaller1.Dispose();
35 }
36 catch (Exception ex)
37 {
38 throw new SysException(ex.Message, ex);
39 }
40 }
41
42 /**//// <summary>
43 /// 卸载Windows服务
44 /// </summary>
45 /// <param name="filepath">程序文件路径</param>
46 public static void UnInstallmyService(string filepath)
47 {
48 try
49 {
50 AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
51 AssemblyInstaller1.UseNewContext = true;
52 AssemblyInstaller1.Path = filepath;
53 AssemblyInstaller1.Uninstall(null);
54 AssemblyInstaller1.Dispose();
55 }
56 catch (Exception ex)
57 {
58 throw new SysException(ex.Message, ex);
59 }
60 }
61
62 /**//// <summary>
63 /// 检查Windows服务是否在运行
64 /// </summary>
65 /// <param name="name">程序的服务名</param>
66 public static bool IsRunning(string name)
67 {
68 bool IsRun = false;
69 try
70 {
71 if (!ServiceIsExisted(name)) return false;
72 var sc = new ServiceController(name);
73 if (sc.Status == ServiceControllerStatus.StartPending || sc.Status == ServiceControllerStatus.Running)
74 {
75 IsRun = true;
76 }
77 sc.Close();
78 }
79 catch
80 {
81 IsRun = false;
82 }
83 return IsRun;
84 }
85
86 /**//// <summary>
87 /// 启动Windows服务
88 /// </summary>
89 /// <param name="name">程序的服务名</param>
90 /// <returns>启动成功返回 true,否则返回 false;</returns>
91 public static bool StarmyService(string name)
92 {
93 try
94 {
95 var sc = new ServiceController(name);
96 if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending)
97 {
98 sc.Start();
99 sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
100 }
101 else
102 {
103
104 }
105 sc.Close();
106 return true;
107 }
108 catch (Exception ex)
109 {
110 throw new SysException(ex.Message, ex);
111 }
112 }
113
114 /**//// <summary>
115 /// 停止Windows服务
116 /// </summary>
117 /// <param name="name">程序的服务名</param>
118 /// <returns>停止成功返回 true,否则返回 false;</returns>
119 public static bool StopmyService(string name)
120 {
121 try
122 {
123 var sc = new ServiceController(name);
124 if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending)
125 {
126 sc.Stop();
127 sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
128 }
129 else
130 {
131
132 }
133 sc.Close();
134 return true;
135 }
136 catch (Exception ex)
137 {
138 throw new SysException(ex.Message, ex);
139 }
140 }
141
142 /**//// <summary>
143 /// 重启Windows服务
144 /// </summary>
145 /// <param name="name">程序的服务名</param>
146 /// <returns>重启成功返回 true,否则返回 false;</returns>
147 public static bool RefreshmyService(string name)
148 {
149 return StopmyService(name) && StarmyService(name);
150 }
151 }
152