MyWindowsService 录音批量下载服务源代码 vs2017 - 思念食品

Form1.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections;
  3. using System.Windows.Forms;
  4. using System.ServiceProcess;
  5. using System.Configuration.Install;
  6. namespace WindowsServiceClient
  7. {
  8. public partial class Form1 : Form
  9. {
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. }
  14. string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe";
  15. string serviceName = "MyService";
  16. //事件:安装服务
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
  20. this.InstallService(serviceFilePath);
  21. }
  22. //事件:启动服务
  23. private void button2_Click(object sender, EventArgs e)
  24. {
  25. if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
  26. }
  27. //事件:停止服务
  28. private void button4_Click(object sender, EventArgs e)
  29. {
  30. if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
  31. }
  32. //事件:卸载服务
  33. private void button3_Click(object sender, EventArgs e)
  34. {
  35. if (this.IsServiceExisted(serviceName))
  36. {
  37. this.ServiceStop(serviceName);
  38. this.UninstallService(serviceFilePath);
  39. }
  40. }
  41. //判断服务是否存在
  42. private bool IsServiceExisted(string serviceName)
  43. {
  44. ServiceController[] services = ServiceController.GetServices();
  45. foreach (ServiceController sc in services)
  46. {
  47. if (sc.ServiceName.ToLower() == serviceName.ToLower())
  48. {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. //安装服务
  55. private void InstallService(string serviceFilePath)
  56. {
  57. using (AssemblyInstaller installer = new AssemblyInstaller())
  58. {
  59. installer.UseNewContext = true;
  60. installer.Path = serviceFilePath;
  61. IDictionary savedState = new Hashtable();
  62. installer.Install(savedState);
  63. installer.Commit(savedState);
  64. }
  65. }
  66. //卸载服务
  67. private void UninstallService(string serviceFilePath)
  68. {
  69. using (AssemblyInstaller installer = new AssemblyInstaller())
  70. {
  71. installer.UseNewContext = true;
  72. installer.Path = serviceFilePath;
  73. installer.Uninstall(null);
  74. }
  75. }
  76. //启动服务
  77. private void ServiceStart(string serviceName)
  78. {
  79. using (ServiceController control = new ServiceController(serviceName))
  80. {
  81. if (control.Status == ServiceControllerStatus.Stopped)
  82. {
  83. control.Start();
  84. }
  85. }
  86. }
  87. //停止服务
  88. private void ServiceStop(string serviceName)
  89. {
  90. using (ServiceController control = new ServiceController(serviceName))
  91. {
  92. if (control.Status == ServiceControllerStatus.Running)
  93. {
  94. control.Stop();
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// 点击窗体最小化时,隐藏任务栏和显示托盘图标
  100. /// </summary>
  101. /// <param name = "sender" ></ param >
  102. /// < param name="e"></param>
  103. private void Form1_Deactivate(object sender, EventArgs e)
  104. {
  105. //当窗体为最小化状态时
  106. if (this.WindowState == FormWindowState.Minimized)
  107. {
  108. this.notifyIcon1.Visible = true; //显示托盘图标
  109. this.Hide();//隐藏窗体
  110. this.ShowInTaskbar = false;//图标不显示在任务栏
  111. }
  112. }
  113. /// <summary>
  114. /// 双击系统托盘的小图标事件
  115. /// </summary>
  116. /// <param name="sender"></param>
  117. /// <param name="e"></param>
  118. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  119. {
  120. if (this.WindowState == FormWindowState.Minimized)
  121. {
  122. this.Show();
  123. this.WindowState = FormWindowState.Normal; //还原窗体
  124. this.ShowInTaskbar = true;//图标显示在任务栏
  125. }
  126. }
  127. /// <summary>
  128. /// 关闭事件
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  133. {
  134. DialogResult result;
  135. result = MessageBox.Show("确定退出吗?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  136. if (result == DialogResult.OK)
  137. {
  138. Application.ExitThread();
  139. }
  140. else
  141. {
  142. e.Cancel = true;
  143. }
  144. }
  145. }
  146. }