CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

304 行
10KB

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