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 |
windows service自动启动相关设置的更多相关文章
- 注册Windows service及其相关
注册Windows service,.net写的 net stop "xxxxxx""%SYSTEMROOT%\Microsoft.NET\Framework\v2.0. ...
- [开发笔记]-Windows Service服务相关注意事项
注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...
- 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils
新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...
- Windows 7 下将 Tomcat Java 程序设置为 Windows Service
方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...
- Windows Unity ARKit发布到IOS相关设置及错误解决
Windows 版Unity安装: 考虑到在虚拟机中运行Unity比较卡,所以采用在Windows Unity上将项目发布好然后再复制到Mac虚拟机中通过XCode进行编译的方式. Unity版本为 ...
- Windows Service 访问远程共享权限设置
最近为实现共享目录之间的文件同步,开发了一个Windows Service. 考虑到在拷贝过程中,如果网络忽然抽风访问不了,导致文件拷贝不完整的情况,果断抛弃.Net 自带的 COPY 方法,而使用D ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- windows Service
用c#中创建一个windows服务非常简单,与windows服务相关的类都在System.ServiceProcess命名空间下. 每个服务都需要继承自ServiceBase类,并重写相应的启动.暂停 ...
- windows Service 创建部署
Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
随机推荐
- unity3d中切换武器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- ruby-gem
gem包管理 ruby的第三方插件是用gem方式来管理,这是一个非常容易发布和共享,一个简单的命令就可以安装上第三方的扩展库 gem install rails 安装ruby on rails gem ...
- Mongo导出数据文件导致错误 Got signal: 6 (Aborted)解决方法
一哥们要导出一个数据表的数据,结果导出一半,硬盘不够用,卡死了, 然后重启主机,导致mongo启动后进程自动死掉, 报错如下. Mon Oct 28 10:39:02.270 [initandlist ...
- 【转】WinForm不同版本覆盖安装
vs2005为winform程序做的安装包.在以有程序旧版本的机子上用新版本的安装包安装软件时提示 “以经安装该产品的另一个版本.无法继续安装此版本........” 在安装部署项目中设“Remove ...
- Java Dns Cache Manipulator
通过代码直接设置Java的DNS(实际上设置的是DNS Cache),支持JDK 6+. 功能 设置/重置DNS(不会再去Lookup DNS) 可以设置单条 或是通过Properties文件批量设 ...
- 任我行 CRM 9.4
下载地址: http://ftp.wecrm.com/azb/graspcrm_ect_v9.4.rar 200用户补丁下载地址: http://bbs.sunwy.org/thread-197862 ...
- DW(四):Azure域控服务器配置
polybase集群要求使用相同的域账号,本节介绍选择用Azure虚拟机搭建自己的内网DNS服务器和域控 创建虚拟网络polybase,创建名为DNS的子网,如下图: 创建虚拟机,选择polybase ...
- mysql查询表中的所有字段
select column_name, table_name from information_schema.columns where table_schema='yizhan' and colum ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(三)
http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2340661.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
- 微博一键分享主要通过对指定 URL 添加各种参数来实现;
微博一键分享主要通过对指定 URL 添加各种参数来实现:也可以用在线生成器自动生成. 示例: 搜狐微博一键分享 URL,只需三个参数: http://t.sohu.com/third/post.jsp ...