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服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
随机推荐
- C#中正则表达式的使用
目前为止,许多编程语言和工具都包含对正则表达式的支持,C#也不例外,C#基础类库中包含有一个命名空间(System.Text.RegularExpressions)和一系列可以充分发挥规则表达式威力的 ...
- android text中显示HTML语言
package com.example.test; import java.io.InputStream; import java.net.URL; import android.annotation ...
- MapReduce: 一个巨大的倒退
前言 databasecolumn 的数据库大牛们(其中包括PostgreSQL的最初伯克利领导:Michael Stonebraker)最近写了一篇评论当前如日中天的MapReduce 技术的文章, ...
- ABBYY FineReader 12PDF选项卡有保存模式吗
PDF是广泛使用的文档格式.在ABBYY Finereader中PDF文档的显示不会因电脑不同而有差异,可加密保护,非常适合在电子存档中进行保存 该选项卡上的保存选项分为以下几类: 默认纸张大小 从下 ...
- JavaScript文件中调用AngularJS内部方法或改变$scope变量
需要在其他JavaScript文件中调用AngularJS内部方法或改变$scope变量,同时还要保持双向数据绑定: 首先获取AngularJS application: 方法一:通过controll ...
- 运行setup.js文件
C:\Windows\System32>wscript.exe setup.js
- 前端页面div float 后高度 height 自适应的问题
最近在画项目页面的时候遇到了一个左侧div一旦加上float:left 属性后,设置其高度height:100% 不起作用,后来网上查了半天也没有找到很好的解决方案,只在csdn里发现了这个马上记录下 ...
- 解决Oracle安装完成后,随便输入用户名密码只要选择DBA身份就能登陆进去的问题
以sysdba身份登录既采用的是本地(系统)认证方式, 将%ORACLE_HOME%\network\admin\sqlnet.ora中的SQLNET.AUTHENTICATION_SERVICES= ...
- 转载:使用sklearn做单机特征工程
目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1.1 标准化 2.1.2 区间缩放法 2.1.3 标准化与归一化的区别 2.2 对定量特征二值化 2.3 对定性特征哑编码 2.4 缺 ...
- oracle定时任务JOB
在jobs上点新建 what值:statisticsToDay; 这个是存储过程的名字间隔:sysdate+1/24 表示每个小时运行一次 1:每分钟执行 Interval ...

