使用DOS進程開啟服務
設置serviceProcessInstaller1控件的Account屬性為“LocalSystem”
設置serviceInstaller1控件的StartType屬性為"Automatic"
在服務器上添加安裝程序,在ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)事件中,添加代碼
1 System.Diagnostics.Process p = new System.Diagnostics.Process();
2 p.StartInfo.FileName = "cmd.exe";
3 p.StartInfo.UseShellExecute = false;
4 p.StartInfo.RedirectStandardInput = true;
5 p.StartInfo.RedirectStandardOutput = true;
6 p.StartInfo.RedirectStandardError = true;
7 p.StartInfo.CreateNoWindow = true;
8 p.Start();
9 string Cmdstring = "sc start myservice"; //CMD命令
10 p.StandardInput.WriteLine(Cmdstring);
11 p.StandardInput.WriteLine("exit");