找到serviceInstaller,在事件AfterInstall中执行cmd 命令,启动服务
using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; namespace yournamespace { [RunInstaller(true)] public partial class Installer : System.Configuration.Install.Installer { private Process p = new Process(); public Installer() { InitializeComponent(); 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(); } private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) { string start = "sc start yourservicename"; p.StandardInput.WriteLine(start); p.StandardInput.WriteLine("exit"); } private void ServiceInstaller_BeforeUninstall(object sender, InstallEventArgs e) { string stop = "sc stop yourservicename"; p.StandardInput.WriteLine(stop); p.StandardInput.WriteLine("exit"); } } }
评论前必须登录!
注册