CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frmServiceConfig.cs 10KB

4 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using CnasSynchrousModel;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Configuration.Install;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.ServiceProcess;
  15. using System.Text;
  16. using System.Windows.Forms;
  17. using System.Xml.Serialization;
  18. namespace CNAS_DBSync
  19. {
  20. public partial class frmServiceConfig : Form
  21. {
  22. ServiceConfig serviceConfig = new ServiceConfig();
  23. public frmServiceConfig()
  24. {
  25. InitializeComponent();
  26. }
  27. private void frmServiceConfig_Load(object sender, EventArgs e)
  28. {
  29. //1.读取本地配置文件,填充上次配置时间
  30. try
  31. {
  32. XmlSerializer serializer = new XmlSerializer(serviceConfig.GetType());
  33. FileStream stream = new FileStream(FileHelper.getBasePath() + "/DataConfig/SyncServiceData.xml", FileMode.Open);
  34. serviceConfig = (ServiceConfig)serializer.Deserialize(stream);
  35. stream.Close();
  36. this.numService.Value = serviceConfig.Minutes;
  37. this.dtpSource.Value = serviceConfig.InitalDT;
  38. this.cbxAutoStart.Checked = getServiceStartType("SyncService") == 2 ? true : false;
  39. }
  40. catch (Exception ex)
  41. {
  42. AppLog.Error(ex.Message);
  43. }
  44. //2.检查本地是否存在已经启动的服务,初始化btn的显示
  45. string strMsg = IfStartService();
  46. switch (strMsg)
  47. {
  48. case "1":
  49. btnInstallService.Text = "安装服务";
  50. btnRun.Text = "启动服务";
  51. this.numService.Enabled = true;
  52. break;
  53. case "2":
  54. btnInstallService.Text = "卸载服务";
  55. btnRun.Text = "启动服务";
  56. this.numService.Enabled = true;
  57. break;
  58. case "3":
  59. btnInstallService.Text = "卸载服务";
  60. btnRun.Text = "停止服务";
  61. this.numService.Enabled = false;
  62. break;
  63. }
  64. }
  65. private string IfStartService()
  66. {
  67. ServiceController[] service = ServiceController.GetServices();
  68. string strMsg = "1";
  69. for (int i = 0; i < service.Length; i++)
  70. {
  71. if (service[i].DisplayName.ToUpper().Equals("SyncService".ToUpper()))
  72. {
  73. strMsg = "2";
  74. if (service[i].Status == ServiceControllerStatus.Running)
  75. {
  76. strMsg = "3";
  77. break;
  78. }
  79. }
  80. }
  81. return strMsg;
  82. }
  83. private void btnRun_Click(object sender, EventArgs e)
  84. {
  85. if (btnRun.Text == "启动服务") //此时启动服务
  86. {
  87. if (this.numService.Value < 1)
  88. {
  89. MessageBox.Show("服务至少每隔1分钟执行");
  90. return;
  91. }
  92. serviceConfig.Minutes = (Int32)this.numService.Value;
  93. serviceConfig.InitalDT = this.dtpSource.Value;
  94. //修改服务为开启启动
  95. if(cbxAutoStart.Checked)
  96. ChangeServiceStartType(2, "SyncService");
  97. if (StartService())
  98. {
  99. SaveServiceConfig(serviceConfig); //存储配置文件
  100. btnRun.Text = "停止服务";
  101. }
  102. else
  103. {
  104. MessageBox.Show("启动服务失败!");
  105. }
  106. }
  107. else //此时停止服务
  108. {
  109. if (StopService())
  110. {
  111. if (cbxAutoStart.Checked)
  112. {
  113. ChangeServiceStartType(3, "SyncService");
  114. cbxAutoStart.Checked = false;
  115. }
  116. btnRun.Text = "启动服务";
  117. }
  118. else
  119. {
  120. MessageBox.Show("停止服务失败!");
  121. }
  122. }
  123. }
  124. private bool StartService()
  125. {
  126. bool bIfSuccess = true;
  127. try
  128. {
  129. ServiceController serviceController = new ServiceController("SyncService");
  130. serviceController.Start();
  131. serviceController.WaitForStatus(ServiceControllerStatus.Running);
  132. serviceController.Close();
  133. }
  134. catch (Exception ex)
  135. {
  136. bIfSuccess = false;
  137. AppLog.Error(ex.Message);
  138. }
  139. return bIfSuccess;
  140. }
  141. private bool StopService()
  142. {
  143. bool bIfSuccess = true;
  144. try
  145. {
  146. ServiceController serviceController = new ServiceController("SyncService");
  147. serviceController.Stop();
  148. serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
  149. serviceController.Close();
  150. }
  151. catch (Exception ex)
  152. {
  153. bIfSuccess = false;
  154. AppLog.Error(ex.Message);
  155. }
  156. return bIfSuccess;
  157. }
  158. private void SaveServiceConfig(ServiceConfig config)
  159. {
  160. try
  161. {
  162. XmlSerializer serializer = new XmlSerializer(config.GetType());
  163. TextWriter writer = new StreamWriter(FileHelper.getBasePath() + "/DataConfig/SyncServiceData.xml");
  164. serializer.Serialize(writer, config);
  165. writer.Close();
  166. }
  167. catch (Exception ex)
  168. {
  169. AppLog.Error(ex.Message);
  170. }
  171. }
  172. /// <summary>
  173. /// 修改服务的启动项 2为自动,3为手动
  174. /// </summary>
  175. /// <param name="startType"></param>
  176. /// <param name="serviceName"></param>
  177. /// <returns></returns>
  178. public static bool ChangeServiceStartType(int startType, string serviceName)
  179. {
  180. try
  181. {
  182. RegistryKey regist = Registry.LocalMachine;
  183. RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
  184. RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
  185. RegistryKey services = currentControlSet.OpenSubKey("Services");
  186. RegistryKey servicesName = services.OpenSubKey(serviceName, true);
  187. servicesName.SetValue("Start", startType);
  188. }
  189. catch (Exception ex)
  190. {
  191. AppLog.Error(ex.Message);
  192. return false;
  193. }
  194. return true;
  195. }
  196. /// <summary>
  197. /// 获取服务启动类型
  198. /// </summary>
  199. /// <param name="serviceName"></param>
  200. /// <returns></returns>
  201. public static int getServiceStartType(string serviceName)
  202. {
  203. int startType = 0;
  204. try
  205. {
  206. RegistryKey regist = Registry.LocalMachine;
  207. RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
  208. RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
  209. RegistryKey services = currentControlSet.OpenSubKey("Services");
  210. RegistryKey servicesName = services.OpenSubKey(serviceName, true);
  211. if(servicesName != null)
  212. startType=(int)servicesName.GetValue("Start");
  213. }
  214. catch (Exception ex)
  215. {
  216. AppLog.Error(ex.Message);
  217. return 0;
  218. }
  219. return startType;
  220. }
  221. private void btnInstallService_Click(object sender, EventArgs e)
  222. {
  223. string serviceFilePath = FileHelper.getBasePath() + @"\CNAS_SyncService.exe";
  224. if (btnInstallService.Text == "安装服务")
  225. {
  226. if (InstallService(serviceFilePath))
  227. {
  228. btnInstallService.Text = "卸载服务";
  229. }
  230. else
  231. {
  232. MessageBox.Show("安装服务失败。");
  233. }
  234. }
  235. else if (btnInstallService.Text == "卸载服务")
  236. {
  237. if (UninstallService(serviceFilePath))
  238. {
  239. btnInstallService.Text = "安装服务";
  240. btnRun.Text = "启动服务";
  241. }
  242. else
  243. {
  244. MessageBox.Show("卸载服务失败。");
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// 安装服务
  250. /// </summary>
  251. /// <param name="serviceFilePath"></param>
  252. private bool InstallService(string serviceFilePath)
  253. {
  254. bool bIfSuccess = true;
  255. try
  256. {
  257. using (AssemblyInstaller installer = new AssemblyInstaller())
  258. {
  259. installer.UseNewContext = true;
  260. installer.Path = serviceFilePath;
  261. IDictionary savedState = new Hashtable();
  262. installer.Install(savedState);
  263. installer.Commit(savedState);
  264. }
  265. }
  266. catch (Exception ex)
  267. {
  268. bIfSuccess = false;
  269. AppLog.Error(ex.Message);
  270. }
  271. return bIfSuccess;
  272. }
  273. /// <summary>
  274. /// 卸载服务
  275. /// </summary>
  276. /// <param name="serviceFilePath"></param>
  277. private bool UninstallService(string serviceFilePath)
  278. {
  279. bool bIfSuccess = true;
  280. try
  281. {
  282. using (AssemblyInstaller installer = new AssemblyInstaller())
  283. {
  284. installer.UseNewContext = true;
  285. installer.Path = serviceFilePath;
  286. installer.Uninstall(null);
  287. }
  288. }
  289. catch (Exception ex)
  290. {
  291. bIfSuccess = false;
  292. AppLog.Error(ex.Message);
  293. }
  294. return bIfSuccess;
  295. }
  296. }
  297. }