服务写完后修改机会很少,老是忘记服务安装完后自动启动

找到Installer的AfterInstall事件。


注意是System.Configuration.Install.Installer下的事件


代码:

System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
//设定程序名 
MyProcess.StartInfo.FileName = "cmd.exe";
//关闭Shell的使用 
MyProcess.StartInfo.UseShellExecute = false;
//重定向标准输入 
MyProcess.StartInfo.RedirectStandardInput = true;
//重定向标准输出 
MyProcess.StartInfo.RedirectStandardOutput = true;
//重定向错误输出 
MyProcess.StartInfo.RedirectStandardError = true;
//设置不显示窗口 
MyProcess.StartInfo.CreateNoWindow = false;
//执行cmd命令 
MyProcess.Start();
MyProcess.StandardInput.WriteLine("net start SocketServer");
MyProcess.StandardInput.WriteLine("exit");
MyProcess.WaitForExit();
原理就是安装完后运行以下CMD命令,并且隐藏下CMD界面