| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System;
- using System.Collections;
- using System.Windows.Forms;
- using System.ServiceProcess;
- using System.Configuration.Install;
- namespace WindowsServiceClient
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe";
- string serviceName = "MyService";
- //事件:安装服务
- private void button1_Click(object sender, EventArgs e)
- {
- if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
- this.InstallService(serviceFilePath);
- }
- //事件:启动服务
- private void button2_Click(object sender, EventArgs e)
- {
- if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
- }
- //事件:停止服务
- private void button4_Click(object sender, EventArgs e)
- {
- if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
- }
- //事件:卸载服务
- private void button3_Click(object sender, EventArgs e)
- {
- if (this.IsServiceExisted(serviceName))
- {
- this.ServiceStop(serviceName);
- this.UninstallService(serviceFilePath);
- }
- }
- //判断服务是否存在
- private bool IsServiceExisted(string serviceName)
- {
- ServiceController[] services = ServiceController.GetServices();
- foreach (ServiceController sc in services)
- {
- if (sc.ServiceName.ToLower() == serviceName.ToLower())
- {
- return true;
- }
- }
- return false;
- }
- //安装服务
- private void InstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- IDictionary savedState = new Hashtable();
- installer.Install(savedState);
- installer.Commit(savedState);
- }
- }
- //卸载服务
- private void UninstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- installer.Uninstall(null);
- }
- }
- //启动服务
- private void ServiceStart(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Stopped)
- {
- control.Start();
- }
- }
- }
- //停止服务
- private void ServiceStop(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Running)
- {
- control.Stop();
- }
- }
- }
-
- /// <summary>
- /// 点击窗体最小化时,隐藏任务栏和显示托盘图标
- /// </summary>
- /// <param name = "sender" ></ param >
- /// < param name="e"></param>
- private void Form1_Deactivate(object sender, EventArgs e)
- {
- //当窗体为最小化状态时
- if (this.WindowState == FormWindowState.Minimized)
- {
- this.notifyIcon1.Visible = true; //显示托盘图标
- this.Hide();//隐藏窗体
- this.ShowInTaskbar = false;//图标不显示在任务栏
- }
- }
- /// <summary>
- /// 双击系统托盘的小图标事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- this.Show();
- this.WindowState = FormWindowState.Normal; //还原窗体
- this.ShowInTaskbar = true;//图标显示在任务栏
- }
- }
- /// <summary>
- /// 关闭事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- DialogResult result;
- result = MessageBox.Show("确定退出吗?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (result == DialogResult.OK)
- {
- Application.ExitThread();
- }
- else
- {
- e.Cancel = true;
- }
- }
- }
- }
|