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 |
转载于:https://www.cnblogs.com/TJessica/p/6066179.html
windows service自动启动相关设置相关推荐
- 用 VC++ 2008 编写 Windows Service(系统服务)
用 VC++ 2008 编写 Windows Service(系统服务) 2008-03-30 08:08 现在许多 Windows Service 应用都可以用 c# 很好的完成,不过毕竟是托管代码 ...
- C#开发Windows Service程序
Windows Service概念介绍 Windows Service,也称Windows服务,是32位Windows操作系统中一种长期运行的后台程序.它们长期后台运行,没有用户界面,默默无闻,但它们 ...
- C# 编写Windows Service(windows服务程序)
Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
- Windows Service编程
Abstract 本文主要介绍 Windows service 的编程模式和 SCM 的相关功能. Content 基础知识 What is Windows Service SCM Service ...
- 用VS(c#)创建、调试windows service以及部署卸载
同事问到windows service的东东,现在整理一下,用c#如何创建一个windows service,以及如何调试.部署.卸载. 一.创建windows service 1. 打开VS2008 ...
- mysql安装提示create_MySQL5.1安装时出现Cannot create windows service for mysql.error:0
安装MySQL5.1过程中,我把以前MySQL5.0的GUI工具和服务器全部删掉,安装目录全部删掉,数据文件目录名字改掉,注册表用完美卸载清理了. 然后重启安装MySQL5.1(我以前遇到过服务启动不 ...
- 什么是Windows Service应用程序?(转)
什么是Windows Service应用程序? Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务 ...
- Windows Service:用C#创建Windows Service
现在的.NET框架已经为Windows service的开发提供足够强大的支持,你只需要关注service所要实现的逻辑,而完全不用关心service底层是如何实现的,相比以前用MFC来说,真是质的飞 ...
- wince 自动启动程序设置
这是我在做wince6的时候为设置自启动而寻找的方法.我发现公司定的这个设备在"Program Files"和windows文件夹下放置的东西在机子重启之后就什么都没有了.我只有使 ...
最新文章
- java 算法练习题
- 查看磁盘空间,清理缓存
- Caffe官方教程翻译(2):Web demo
- 远程连接CentOS的MySQL报错:Can't connect to MySQL server on 'XXX' (13)
- python3实现抓取网页资源的 N 种方法(内附200GPython学习资料)
- javascript中编码与解码的decodeURI()、decodeURIComponent()区别
- html5 斜率画线,一次函数的斜率与图像的关系
- c语言跳至表域跳几个字符,c语言学习最好资料.doc
- android windowsoftinputmode 状态栏冲突,android:windowSoftInputMode属性
- Atitit sql查询语法 SQL SELECT完整语法	3 1.SELECT语法	3 2.FROM子句	5 3.WHERE子句	6 下面两张表将在后面使用到	7 1.比较运算符	7 2.逻辑
- ddtek.oracle.dll使用相关
- 谷歌地图的级别与对应比例尺及分辨率探究
- C#阿里云短信接口API开发步骤
- C语言lo如何优化运行界面,高质量程序优化总结整理【经典】
- tableau之仪表板与故事
- 实现从淘宝(天猫)定时抓取订单数据、打印电子面单并保存到ERP表中
- dango 自带的用户认证
- key_t键和ftok函数
- Thymeleaf全解
- hue执行workflow工作流出现直接FAILED
热门文章
- liunx导出mysql慢查询日志查看_查看 MySQL 慢查询日志文件-问答-阿里云开发者社区-阿里云...
- 离线人脸属性分析_最新开源 | 人脸识别应用套件:毫秒级离线识别 适用多类场景...
- HDLBits 系列(36)Arbitration circuit implemented by FSM
- Spring学习总结(6)——Spring之核心容器bean
- javaweb面试一
- 每天一个设计模式之组合模式
- 滚动条的出现导致居中的元素会晃动
- IDC公布2017年亚太地区数据中心10大预测
- 赚钱的这些年(上)苦逼
- 简单的视频采集demo