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.

310 lines
10KB

  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() + "/Data/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() + "/Data/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. startType=(int)servicesName.GetValue("Start");
  212. }
  213. catch (Exception ex)
  214. {
  215. AppLog.Error(ex.Message);
  216. return 0;
  217. }
  218. return startType;
  219. }
  220. private void btnInstallService_Click(object sender, EventArgs e)
  221. {
  222. string serviceFilePath = FileHelper.getBasePath() + @"\CNAS_SyncService.exe";
  223. if (btnInstallService.Text == "安装服务")
  224. {
  225. if (InstallService(serviceFilePath))
  226. {
  227. btnInstallService.Text = "卸载服务";
  228. }
  229. else
  230. {
  231. MessageBox.Show("安装服务失败。");
  232. }
  233. }
  234. else if (btnInstallService.Text == "卸载服务")
  235. {
  236. if (UninstallService(serviceFilePath))
  237. {
  238. btnInstallService.Text = "安装服务";
  239. btnRun.Text = "启动服务";
  240. }
  241. else
  242. {
  243. MessageBox.Show("卸载服务失败。");
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// 安装服务
  249. /// </summary>
  250. /// <param name="serviceFilePath"></param>
  251. private bool InstallService(string serviceFilePath)
  252. {
  253. bool bIfSuccess = true;
  254. try
  255. {
  256. using (AssemblyInstaller installer = new AssemblyInstaller())
  257. {
  258. installer.UseNewContext = true;
  259. installer.Path = serviceFilePath;
  260. IDictionary savedState = new Hashtable();
  261. installer.Install(savedState);
  262. installer.Commit(savedState);
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. bIfSuccess = false;
  268. AppLog.Error(ex.Message);
  269. }
  270. return bIfSuccess;
  271. }
  272. /// <summary>
  273. /// 卸载服务
  274. /// </summary>
  275. /// <param name="serviceFilePath"></param>
  276. private bool UninstallService(string serviceFilePath)
  277. {
  278. bool bIfSuccess = true;
  279. try
  280. {
  281. using (AssemblyInstaller installer = new AssemblyInstaller())
  282. {
  283. installer.UseNewContext = true;
  284. installer.Path = serviceFilePath;
  285. installer.Uninstall(null);
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. bIfSuccess = false;
  291. AppLog.Error(ex.Message);
  292. }
  293. return bIfSuccess;
  294. }
  295. }
  296. }