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自动启动相关设置的更多相关文章

  1. 注册Windows service及其相关

    注册Windows service,.net写的 net stop "xxxxxx""%SYSTEMROOT%\Microsoft.NET\Framework\v2.0. ...

  2. [开发笔记]-Windows Service服务相关注意事项

    注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...

  3. 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils

    新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...

  4. Windows 7 下将 Tomcat Java 程序设置为 Windows Service

    方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...

  5. Windows Unity ARKit发布到IOS相关设置及错误解决

    Windows 版Unity安装: 考虑到在虚拟机中运行Unity比较卡,所以采用在Windows Unity上将项目发布好然后再复制到Mac虚拟机中通过XCode进行编译的方式. Unity版本为 ...

  6. Windows Service 访问远程共享权限设置

    最近为实现共享目录之间的文件同步,开发了一个Windows Service. 考虑到在拷贝过程中,如果网络忽然抽风访问不了,导致文件拷贝不完整的情况,果断抛弃.Net 自带的 COPY 方法,而使用D ...

  7. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  8. windows Service

    用c#中创建一个windows服务非常简单,与windows服务相关的类都在System.ServiceProcess命名空间下. 每个服务都需要继承自ServiceBase类,并重写相应的启动.暂停 ...

  9. windows Service 创建部署

    Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...

随机推荐

  1. mysql学习之-逻辑架构

    mysql逻辑架构图: 第一层,服务层(为客户端服务):为请求做连接处理,授权认证,安全等. 1.  每个连接的查询都在一个进程中的线程完成. 2.  服务器负责缓存线程,所以服务层不需要为每个连接新 ...

  2. 执行bat文件

    set CLASSPATH=E:\kuaipan\work\J2SE_workspace\JavaSEbasic\bin;E:\kuaipan\study\jar\jxl\*; set HEAP=-X ...

  3. postgreSQL 时间线

    “时间线”(Timeline)是PG一个很有特色的概念,在备份恢复方面的文档里面时有出现.但针对这个概念的详细解释却很少,也让人不太好理解,我们在此仔细解析一下. 时间线的引入 为了理解引入时间线的背 ...

  4. oracle闪回使用以及删除存储过程恢复

    oracle恢复删除的数据  恢复删除的存储过程 SELECT * FROM dba_source as of timestamp (systimestamp -interval'600'second ...

  5. ABBYY FineReader无法打开TWAIN源怎么办

    ABBYY FineReader OCR文字识别软件不仅可以将PDF文档和图像文件(包括数码照片)转换为可编辑.可搜索的格式,还可以用来扫描文档,但在扫描过程中,有时可能会出现以下两种错误信息:一是无 ...

  6. SQL Server 表所在的文件组

    SELECT  o.[name] ,-- o.[type], i.[name], i.[index_id],         f.[name] FROM    sys.indexes i        ...

  7. 019. Asp.net将SqlServer中的数据保存到xls/txt中

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  8. JavaScript 全选函数的实现

    Html代码: <table id="purchase-info" class="table table-bordered table-hover table-st ...

  9. DB2 组内分组排序,游标使用

    CREATE PROCEDURE Sys_Init_tblaccountsuser_sortid () P1: BEGIN '; ; ; ; DECLARE CUR1 CURSOR WITH RETU ...

  10. 剑指offer系列52---约瑟夫环问题

    [题目]0,1,...n排成一个圈,从0开始每次删除第m个数,求圆圈最后个数. * [思路]1 用数组模拟圆圈.当数到最后一个数即index==n时,令index==0 重头开始遍历: * 当遇到已经 ...