服務啓動
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
服務組件
serivice.cs 鼠標右鍵添加安裝程序
- serviceProcessInstaller1 的賬戶設置成localsystem否則安裝時提示密碼登錄
- serviceInstaller1服務的名稱配置
執行安裝
winform不支持服務帶參數安裝,只有用winapi添加帶參數的服務
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WindowsService.exe");
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
}
刪除
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = Application.ExecutablePath;
in