首页 > 代码库 > C# WindowsService安装与卸载
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。