windows service自动启动相关设置
| http://blog.csdn.net/thebesttome/article/details/7590025 |
| 原帖:http://www.cnblogs.com/junior/archive/2012/03/14/2396620.html |
| 首先设置下两个控件: |
| 设置serviceProcessInstaller1控件的Account属性为“LocalSystem” |
| 设置serviceInstaller1控件的StartType属性为"Automatic" |
| 然后设置ProjectInstaller(默认名)的事件AfterInstall和BeforeUninstall,分别用于在制作安装程序时自动启动和卸载时自动关闭。 |
| 详细代码如下: |
| public partial class ProjectInstaller : Installer |
| { |
| private Process p = new Process(); |
| public ProjectInstaller() |
| { |
| InitializeComponent(); |
| 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(); |
| } |
| private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) |
| { |
| string Cmdstring = "sc start LanMsgService"; //CMD命令 |
| p.StandardInput.WriteLine(Cmdstring); |
| p.StandardInput.WriteLine("exit"); |
| } |
| private void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e) |
| { |
| string Cmdstring = "sc stop LanMsgService"; //CMD命令 |
| p.StandardInput.WriteLine(Cmdstring); |
| p.StandardInput.WriteLine("exit"); |
| } |
| } |
| 如上,则可在安装服务后立刻启动windows服务。 |
| 安装程序制作: |
| 安装项目(右击) —> 视图 —> 文件系统。由于我们是安装服务,就不需要用户桌面和程序菜单了,直接应用程序文件夹(右击)—> 增加 —> 项目输出。这一步增加了安装程序的文件夹,下一步就是给这个安装程序增加操作,这里我们增加两个基本操作,一个是安装,一个是卸载。安装项目(右击) —> 视图 —> 自定义操作。上面可以看到有安装,提交,回滚,卸载等操作,我们先增加安装操作,安装(右击)—> 增加自定义操作,会弹出一个对话,选择应用程序文件夹,并选中之前增加的主输出项,确定,这样这个安装程序就增加了安装的操作,同样按照这样的方式增加卸载操作,卸载与安装唯一不同的是需要设置一个命令参数,不可少,在Arguments 里输入 /u 表示卸载命令相当于 InstallUtil.exe /u 服务路径 , 到这里 ,我们的安装程序就已经制作好了,生成安装程序项目,将会生成 setup.exe 和 setup.msi 安装文件,拷贝到客户端,点击setup.exe 就能安装我们的服务。 |
| http://www.tuicool.com/articles/ArqAre |
| .Net实现Windows服务安装完成后自动启动的两种方法 |
| 考虑到部署方便,我们一般都会将C#写的Windows服务制作成安装包。在服务安装完成以后,第一次还需要手动启动服务,这样非常不方便。 |
| 方法一: 在安装完成事件里面调用命令行的方式启动服务 |
| 此操作之前要先设置下两个控件 |
| 设置serviceProcessInstaller1控件的Account属性为“ LocalSystem ” |
| 设置serviceInstaller1控件的StartType属性为" Automatic " |
| 在服务器上添加安装程序,在 private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) 事件中,添加以下代码: |
| ///<summary> |
| /// 安装后自动启动服务 |
| ///</summary> |
| ///<param name="sender"></param> |
| ///<param name="e"></param> |
| privatevoidProjectInstaller_AfterInstall(object sender, InstallEventArgs e) |
| { |
| Process p = new Process |
| { |
| StartInfo = |
| { |
| FileName = "cmd.exe", |
| UseShellExecute = false, |
| RedirectStandardInput = true, |
| RedirectStandardOutput = true, |
| RedirectStandardError = true, |
| CreateNoWindow = true |
| } |
| }; |
| p.Start(); |
| conststring cmdString = "sc start 银医通服务平台1.0"; //cmd命令,银医通服务平台1.0为服务的名称 |
| p.StandardInput.WriteLine(cmdString); |
| p.StandardInput.WriteLine("exit"); |
| } |
| 查阅了网上的一些资料,这种方式虽可行,但觉得不够完美。好了,下面来看看如何更好地做到服务自动启动。 |
| 方法二:使用ServiceController对象 |
| 1.重写ProjectInstaller的Commit方法 |
| using System; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.ComponentModel; |
| using System.Configuration.Install; |
| using System.Linq; |
| using System.ServiceProcess; |
| namespace CleanExpiredSessionSeivice |
| { |
| [RunInstaller(true)] |
| public partial class ProjectInstaller : System.Configuration.Install.Installer |
| { |
| publicProjectInstaller() |
| { |
| InitializeComponent(); |
| } |
| public override voidCommit(IDictionary savedState) |
| { |
| base.Commit(savedState); |
| ServiceController sc = new ServiceController("银医通服务平台1.0"); |
| if(sc.Status.Equals(ServiceControllerStatus.Stopped)) |
| { |
| sc.Start(); |
| } |
| } |
| } |
| } |
| 2、在服务安装项目中添加名为 Commit的 Custome Action |
| 在服务安装项目上右击,在弹出的菜单中选择View — Custom Actions |
![]() |
| 然后在Commit项上右击,选择Add Custom Action…,在弹出的列表框中选择Application Folder。最终结果如下: |
![]() |
| 需要注意的是,第二步操作是必不可少的,否则服务无法自动启动。我的个人理解是Commit Custom Action 会自动调用ProjectInstaller的Commit方法,Commit Custom Action 在这里扮演了一个调用者的角色。 |
| http://blog.csdn.net/vvhesj/article/details/8349615 |
弄丢的小可爱🌸



浙公网安备 33010602011771号