在服务上添加安装程序,在serviceProcessInstaller1控件的Committed事件当中添加代码:

            System.ServiceProcess.ServiceController     controller     =     new     System.ServiceProcess.ServiceController("MyService");  
            controller.Start();  

MyService为服务名


此操作之前要先设置下两个控件

设置serviceProcessInstaller1控件的Account属性为“LocalSystem
设置serviceInstaller1控件的StartType属性为"Automatic"

 

以上代码有误,待解决后再修改

----------------华丽的分割线--------------------------

在服务器上添加安装程序,在private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)事件中,添加以下代码:

 

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 myservice"//CMD命令
p.StandardInput.WriteLine(Cmdstring);
p.StandardInput.WriteLine(
"exit");

即可在安装服务后立刻启动windows服务

 

注: 引用using System.Diagnostics;//调用Process

 

以上代码,亲测成功! 欢迎点评(在网上找了n久解决方案,最后还是自己搞定,转载请注明出处)

相关文章:

  • 2022-12-23
  • 2021-06-04
  • 2022-01-07
  • 2021-11-12
  • 2022-12-23
  • 2022-01-08
  • 2021-04-22
猜你喜欢
  • 2022-12-23
  • 2021-06-25
  • 2021-09-20
  • 2021-10-03
  • 2021-09-19
  • 2022-01-20
  • 2021-06-25
相关资源
相似解决方案