C# 安装布署 及Windows服务自动启动

来源:互联网 发布:日剧台词 知乎 编辑:程序博客网 时间:2024/05/21 10:50

 

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

 

C#安装布署

建立一个新的安装项目ServerSetup(为刚才那个服务建立一个安装项目) 
右键-添加-项目输出-主输出-选择Service1-确定 
右键-视图-自定义操作-自定义操作上右键-添加自定义操作-打开应用程序文件夹-选择刚才那个主输出-确定 
重新生成这个安装项目-右键-安装 

 

 

查看服务,,则自已的服务("myservice")已自动启动

原创粉丝点击