|
- // MesServiceTool.Form1
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Configuration.Install;
- using System.Drawing;
- using System.ServiceProcess;
- using System.Windows.Forms;
- public class Form1 : Form
- {
- private IContainer components;
- private Button btn_install;
- private Button btn_uninstall;
- private Button btn_start;
- private Button btn_stop;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- if (IsServiceExisted())
- {
- btn_install.Enabled = false;
- return;
- }
- btn_uninstall.Enabled = false;
- btn_start.Enabled = false;
- btn_stop.Enabled = false;
- }
- private void btn_install_Click(object sender, EventArgs e)
- {
- if (InstallService())
- {
- MessageBox.Show("服务安装成功", "服务安装");
- btn_uninstall.Enabled = true;
- btn_start.Enabled = true;
- btn_stop.Enabled = true;
- btn_install.Enabled = false;
- }
- else
- {
- MessageBox.Show("服务安装失败", "服务安装");
- }
- }
- private void btn_uninstall_Click(object sender, EventArgs e)
- {
- if (StopService() && UnInstallService())
- {
- MessageBox.Show("服务卸载成功", "服务卸载");
- btn_install.Enabled = true;
- btn_start.Enabled = false;
- btn_stop.Enabled = false;
- btn_uninstall.Enabled = false;
- }
- else
- {
- MessageBox.Show("服务卸载失败", "服务卸载");
- }
- }
- private void btn_start_Click(object sender, EventArgs e)
- {
- if (StartService())
- {
- MessageBox.Show("服务开启成功", "服务开启");
- }
- else
- {
- MessageBox.Show("服务开启失败", "服务开启");
- }
- }
- private void btn_stop_Click(object sender, EventArgs e)
- {
- if (StopService())
- {
- MessageBox.Show("服务停止成功", "服务停止");
- }
- else
- {
- MessageBox.Show("服务停止失败", "服务停止");
- }
- }
- private bool IsServiceExisted()
- {
- ServiceController[] services = ServiceController.GetServices();
- for (int i = 0; i < services.Length; i++)
- {
- if (services[i].ServiceName.ToLower() == "LCMesService".ToLower())
- {
- return true;
- }
- }
- return false;
- }
- private bool InstallService()
- {
- try
- {
- using AssemblyInstaller assemblyInstaller = new AssemblyInstaller();
- assemblyInstaller.UseNewContext = true;
- assemblyInstaller.Path = Application.StartupPath + "\\MesService.exe";
- IDictionary dictionary = new Hashtable();
- assemblyInstaller.Install(dictionary);
- assemblyInstaller.Commit(dictionary);
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- private bool UnInstallService()
- {
- try
- {
- using AssemblyInstaller assemblyInstaller = new AssemblyInstaller();
- assemblyInstaller.UseNewContext = true;
- assemblyInstaller.Path = Application.StartupPath + "\\MesService.exe";
- assemblyInstaller.Uninstall(null);
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- private bool StartService()
- {
- try
- {
- using ServiceController serviceController = new ServiceController("LCMesService");
- if (serviceController.Status == ServiceControllerStatus.Stopped)
- {
- serviceController.Start();
- }
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- private bool StopService()
- {
- try
- {
- using ServiceController serviceController = new ServiceController("LCMesService");
- if (serviceController.Status == ServiceControllerStatus.Running)
- {
- serviceController.Stop();
- }
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing && components != null)
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- btn_install = new System.Windows.Forms.Button();
- btn_uninstall = new System.Windows.Forms.Button();
- btn_start = new System.Windows.Forms.Button();
- btn_stop = new System.Windows.Forms.Button();
- SuspendLayout();
- btn_install.Location = new System.Drawing.Point(12, 34);
- btn_install.Name = "btn_install";
- btn_install.Size = new System.Drawing.Size(95, 31);
- btn_install.TabIndex = 0;
- btn_install.Text = "安装";
- btn_install.UseVisualStyleBackColor = true;
- btn_install.Click += new System.EventHandler(btn_install_Click);
- btn_uninstall.Location = new System.Drawing.Point(113, 34);
- btn_uninstall.Name = "btn_uninstall";
- btn_uninstall.Size = new System.Drawing.Size(95, 31);
- btn_uninstall.TabIndex = 1;
- btn_uninstall.Text = "卸载";
- btn_uninstall.UseVisualStyleBackColor = true;
- btn_uninstall.Click += new System.EventHandler(btn_uninstall_Click);
- btn_start.Location = new System.Drawing.Point(262, 34);
- btn_start.Name = "btn_start";
- btn_start.Size = new System.Drawing.Size(95, 31);
- btn_start.TabIndex = 2;
- btn_start.Text = "启动";
- btn_start.UseVisualStyleBackColor = true;
- btn_start.Click += new System.EventHandler(btn_start_Click);
- btn_stop.Location = new System.Drawing.Point(363, 34);
- btn_stop.Name = "btn_stop";
- btn_stop.Size = new System.Drawing.Size(95, 31);
- btn_stop.TabIndex = 3;
- btn_stop.Text = "停止";
- btn_stop.UseVisualStyleBackColor = true;
- btn_stop.Click += new System.EventHandler(btn_stop_Click);
- base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
- base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- base.ClientSize = new System.Drawing.Size(465, 94);
- base.Controls.Add(btn_stop);
- base.Controls.Add(btn_start);
- base.Controls.Add(btn_uninstall);
- base.Controls.Add(btn_install);
- base.MaximizeBox = false;
- base.MinimizeBox = false;
- base.Name = "Form1";
- Text = "MesServer工具";
- base.Load += new System.EventHandler(Form1_Load);
- ResumeLayout(false);
- }
- }
复制代码
|
|