zoukankan      html  css  js  c++  java
  • c# service

    https://www.cnblogs.com/cncc/p/7170951.html

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    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}\Service1.exe";
    string serviceName = "Service1";

    //事件:安装服务


    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    serviceFilePath = tb_path.Text.ToString().Trim();
    if (serviceFilePath == "")
    {
    MessageBox.Show("请输入服务所在路径!");
    return;
    }
    string[] strs = serviceFilePath.Split('\');
    serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
    tb_name.Text = serviceName;
    if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
    this.InstallService(serviceFilePath);
    MessageBox.Show("安装成功!");
    }
    catch (Exception ex)
    {
    MessageBox.Show("安装失败!" + ex.ToString());
    }


    }

    //事件:启动服务
    private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    serviceFilePath = tb_path.Text.ToString().Trim();
    if (serviceFilePath == "")
    {
    MessageBox.Show("请输入服务所在路径!");
    return;
    }
    string[] strs = serviceFilePath.Split('\');
    serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
    tb_name.Text = serviceName;
    if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
    MessageBox.Show("启动成功!");
    }
    catch (Exception ex)
    {
    MessageBox.Show("启动失败!" + ex.ToString());
    }
    }

    //事件:停止服务
    private void button3_Click(object sender, EventArgs e)
    {
    try
    {
    serviceFilePath = tb_path.Text.ToString().Trim();
    if (serviceFilePath == "")
    {
    MessageBox.Show("请输入服务所在路径!");
    return;
    }
    string[] strs = serviceFilePath.Split('\');
    serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
    tb_name.Text = serviceName;
    if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
    MessageBox.Show("停止成功!");
    }
    catch (Exception ex)
    {
    MessageBox.Show("停止失败!" + ex.ToString());
    }
    }

    //事件:卸载服务
    private void button4_Click(object sender, EventArgs e)
    {
    try
    {
    serviceFilePath = tb_path.Text.ToString().Trim();
    if (serviceFilePath == "")
    {
    MessageBox.Show("请输入服务所在路径!");
    return;
    }
    string[] strs = serviceFilePath.Split('\');
    serviceName = (strs[strs.Length - 1]).Substring(0, strs[strs.Length - 1].Length - 4);
    tb_name.Text = serviceName;
    if (this.IsServiceExisted(serviceName))
    {
    this.ServiceStop(serviceName);
    this.UninstallService(serviceFilePath);
    }
    MessageBox.Show("卸载成功!");
    }
    catch (Exception ex)
    {
    MessageBox.Show("卸载失败!" + ex.ToString());
    }
    }

    //判断服务是否存在
    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();
    }
    }
    }
    }

    }

  • 相关阅读:
    TreeList 树形控件 实现带三种状态的CheckBox
    SQL 左外连接,右外连接,全连接,内连接(转)
    在DataTable中进行数据查询 (转)
    uva10594 Data Flow最小费用流,两个代码区别不大(我没看出区别),为什么一个对,另一个超时!!
    SGU142 Keyword好题
    uva 10881
    南京理工1747(数论)WA了好多遍!
    CF161D 树形dp
    uva 11646(大水题几何分类)
    求a加到b二进制加法有多少次进位。
  • 原文地址:https://www.cnblogs.com/Apeak/p/14917800.html
Copyright © 2011-2022 走看看