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服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
随机推荐
- InnoDB Double write
记得刚开始看InnoDB文档的时候,Double Write一节(其实只有一小段)就让我很困惑.无奈当时内力太浅,纠缠了很久也没弄明白.时隔几个月,重新来整理一下. 涉及到的概念:Buffer Poo ...
- docker加速器
https://cr.console.aliyun.com/#/docker/booster 阿里云开发者账号注册后,获得一专属加速器地址. 转发自: http://www.imike.me/2016 ...
- MyBatis入门学习
所需要jar包:mybatis-3.x.x.jar .如果需要和spring整合,还需要加入相关的包 1:看项目目录 红颜色不要关心 2:按照步骤: 1:加入jar包 2:创建数据源(configur ...
- add to svn ignore disabled
The problem is that the folder is already under version control. Here's how I fix this type of probl ...
- linux服务之asterisk
由于Asterisk过于专业且复杂,所以目前也存在大量衍生自Asterisk但简化过的通信系统,以让用户较容易使用.比如在欧美比较流行的elastix.trixbox.或以简体中文为基础的Freeir ...
- maxscript,执行选中代码片段
选中一行或几行代码,然后按数字小键盘上的Enter键,即可单独运行之.此法方便调试.
- Android Afinal框架
项目如图: 本文参考网络! Afinal是一个开源的android的orm和ioc应用开发框架,其特点是小巧灵活,代码入侵量少.在android应用开发中,通过 Afinal的ioc框架,诸如ui绑定 ...
- 关闭Outlook时最小化
公司用的是阿里邮箱,foxmail客户端总是把已读邮件又标记成未读,又赶上最近招聘,真是深受其烦.已经无法忍受了,决定换了它,重新用起最强客户端outlook. Outlook解决了foxmail的问 ...
- web前端面试2
百度另外一部门面试 1.前端html5新特性 2.数组常用函数 3.前后台json树形结构遍历,对算法的了解 4.css3背景属性设置为适合匹配模块的属性是什么 5.jquery插件的写法 6.研究过 ...
- android模拟器不能用键盘
android模拟器不能用键盘?咋整啊?

