1、自动启动的方法设置:
请先设置以下两个控件:
设置serviceProcessInstaller1控件的Account属性为“LocalSystem”.
设置serviceInstaller1控件的StartType属性为"Automatic".
服务上添加安装程序后,通过编码实现自动启动,方法如下:
给serviceProcessInstaller1添加AfterInstall事件,然后添加如下代码:
- private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
- {
- #region Windows服务安装后自动启动
- Process p = new Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- string Cmdstring = "sc start MonitoringService"; //CMD命令,MonitoringService为服务名称,即控件serviceInstaller1的ServiceName属性。
- p.StandardInput.WriteLine(Cmdstring);
- p.StandardInput.WriteLine("exit");
- #endregion
- }
2、设置允许服务与桌面交互方法:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration.Install;
- using Microsoft.Win32;
- using System.Diagnostics;
- namespace Systam
- {
- [RunInstaller(true)]
- public partial class ProjectInstaller : Installer
- {
- public ProjectInstaller()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 设置允许服务与桌面交互 ,修改了注册表,要重启系统才能生效
- /// </summary>
- /// <param name="ServiceName">服务程序名称</param>
- private void SetServiceTable(string ServiceName)
- {
- RegistryKey rk = Registry.LocalMachine;
- string key = @"SYSTEM/CurrentControlSet/Services/" + ServiceName;
- RegistryKey sub = rk.OpenSubKey(key, true);
- int value = (int)sub.GetValue("Type");
- sub.SetValue("Type", value | 256);
- }
- private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
- {
- #region 设置允许服务与桌面交互
- SetServiceTable("MonitoringService");
- #endregion
- #region Windows服务安装后自动启动
- Process p = new Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- string Cmdstring = "sc start MonitoringService"; //CMD命令
- p.StandardInput.WriteLine(Cmdstring);
- p.StandardInput.WriteLine("exit");
- #endregion
- }
- }
- }
参考:
http://www.cnblogs.com/joejoe/archive/2009/05/09/1453250.html
http://www.cnblogs.com/wfwup/archive/2009/01/14/1375382.html
3、方法3:在ProjectInstaller的视图设计器中添加控件ServiceController,即serviceController1,添加serviceInstaller1的AfterInstall事件:
- private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
- {
- ConnectionOptions coOptions = new ConnectionOptions();
- coOptions.Impersonation = ImpersonationLevel.Impersonate;
- ManagementScope mgmtScope = new System.Management.ManagementScope(@"root/CIMV2", coOptions);
- mgmtScope.Connect();
- ManagementObject wmiService;
- wmiService = new ManagementObject("Win32_Service.Name='" + serviceController1.ServiceName + "'");
- ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
- InParam["DesktopInteract"] = true;
- ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
- serviceController1.Start();
- }