winform scm服务

服务启动

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章