C# WindowsService安裝與卸載

來源:互聯網
上載者:User

標籤: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

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.