標籤:des style class blog code java
最近在做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 Program10 {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 else22 {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 Program10 {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 WindowsServiceTest10 {11 [RunInstaller(true)]12 public partial class ProjectInstaller : System.Configuration.Install.Installer13 {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:LocalService11 serviceProcessInstaller1.Parent:ProjectInstaller