Windows服务安装后自动启动

 

在服务上添加安装程序,在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)事件中,添加以下代码:

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->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

其它请参考:http://topic.csdn.net/t/20051130/15/4428801.html

posted @ 2011-11-29 11:23  deepwishly  阅读(197)  评论(0编辑  收藏  举报