服务启动
代码语言:javascript复制static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
服务组件
serivice.cs 鼠标右键添加安装程序
- serviceProcessInstaller1 的账户设置成localsystem否则安装时提示密码登录
- serviceInstaller1服务的名称配置
执行安装
winform不支持服务带参数安装,只有用winapi添加带参数的服务
代码语言:javascript复制 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);
}
删除
代码语言:javascript复制using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = Application.ExecutablePath;
in