• C# WindowsService安装与卸载


    最近在做WinService,总结了一下安装和卸载程序,服务实现内容无法总结了。

    安装程序:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Diagnostics;
     6 
     7 namespace InstallWin
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             if (args.Length > 0)
    14             {
    15                 if (args[0] == "start")
    16                 {
    17                     System.Threading.Thread.Sleep(10000);
    18                     System.Diagnostics.Process.Start("sc start TalentMonitorService");
    19                 }
    20             }
    21             else
    22             {
    23                 string DotnetPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
    24                 string installUtil = DotnetPath + "InstallUtil.exe";
    25                 ProcessStartInfo info = new ProcessStartInfo();
    26                 info.CreateNoWindow = true;
    27                 info.WindowStyle = ProcessWindowStyle.Hidden;
    28                 info.FileName = installUtil;
    29                 info.Arguments = """ + AppDomain.CurrentDomain.BaseDirectory + "WindowsServiceTest.exe"";
    30                 Process pro = Process.Start(info);
    31                 Console.WriteLine("正在安装监控服务...");
    32                 pro.WaitForExit();
    33 
    34                 info.FileName = "net.exe";
    35                 info.Arguments = "start WindowsServiceTest";
    36                 pro = Process.Start(info);
    37                 Console.WriteLine("正在启动监控服务...");
    38                 pro.WaitForExit();
    39             }
    40         }
    41     }
    42 }

    卸载程序:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Diagnostics;
     6 
     7 namespace UnInstall
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string DotnetPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
    14             string installUtil = DotnetPath + "InstallUtil.exe";
    15             ProcessStartInfo info = new ProcessStartInfo();
    16             info.CreateNoWindow = true;
    17             info.WindowStyle = ProcessWindowStyle.Hidden;
    18             info.FileName = "net.exe";
    19             info.Arguments = "stop WindowsServiceTest";
    20             Process pro = Process.Start(info);
    21             Console.WriteLine("正在停止监控服务...");
    22             pro.WaitForExit();
    23 
    24             info.FileName = installUtil;
    25             info.Arguments = "/u "" + AppDomain.CurrentDomain.BaseDirectory + "\WindowsServiceTest.exe"";
    26             pro = Process.Start(info);
    27             Console.Write("正在卸载监控服务...");
    28             pro.WaitForExit();
    29         }
    30     }
    31 }

    简单的服务:

     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.ComponentModel;
     5 using System.Configuration.Install;
     6 using System.Linq;
     7 
     8 
     9 namespace WindowsServiceTest
    10 {
    11     [RunInstaller(true)]
    12     public partial class ProjectInstaller : System.Configuration.Install.Installer
    13     {
    14         public ProjectInstaller()
    15         {
    16             InitializeComponent();
    17         }
    18     }
    19 }
     1 serviceInstaller1.Description:测试服务
     2 
     3 serviceInstaller1.DisPlayName:测试服务
     4 
     5 serviceInstaller1.Parent:ProjectInstaller
     6 
     7 serviceInstaller1.ServiceName:WindowsServiceTest
     8 
     9 
    10 serviceProcessInstaller1.Account:LocalService
    11 serviceProcessInstaller1.Parent:ProjectInstaller
  • 相关阅读:
    Office 2007在安装过程中出错-解决办法
    Sql日期时间格式转换
    大型网站架构演变和知识体系
    作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的
    Java 入门基础
    技术类面试、笔试题汇总
    怎样玩转千万级别的数据
    数据库性能优化:SQL索引
    最后一公里极速配送
    编辑-滴滴算法大赛算法解决过程
  • 原文地址:https://www.cnblogs.com/pyffcwj/p/3772914.html
Copyright © 2020-2023  润新知