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. 使用 NGUI 实现头顶文字及血条

    以下是 NGUI HUD Text 实现的: 基本原理: 1. 在角色头顶绑一个点 Pivot,用于对齐 2. 因为界面总是覆盖在人物头顶信息的上面,所以将 UIRoot 分为2个 Panel:1) ...

  2. C#字符补位

    C#字符补位 .byte类型的字符,用5位2进制数表示,右对齐,不足5位,前面补零. byte b; Convert.ToString(b, ).PadLeft(, ') .byte类型的字符,用2位 ...

  3. Unity3d NGUI的使用(九)(UIScrollView制作滑动列表)

    UIScrollView制作滑动列表,可横向,竖直展示一些列表在固定可视范围内 UIScrollVIew只是一个可滑动的UI组件 如果需要制作复杂的可视区域UI需要配合使用UIPanel与UIGrid ...

  4. SqlServer中quotename用法与实例

    语法 QUOTENAME ( 'character_string' [ , 'quote_character' ] ) 参数 'character_string' Unicode 字符数据构成的字符串 ...

  5. ASP.NET GridView HyperLinkField传值和取值【转】

    来源:http://www.cnblogs.com/junjie94wan/archive/2011/08/17/2143623.html 经常做Winform程序,好久没有做WEB都有些生疏了,Gr ...

  6. 2的N次方 【转】

    题目的链接为:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1009 题目为: 2的N次 ...

  7. 简单实现JS Loading功能

    我们经常在浏览网页的时候会看到数据在加载时,出现的LOADING提示.其实这个功能原理是很简单的,就是一个DIV遮盖当前页面,然后Loading就在遮盖DIV层上展示出来,现在我们来动手实现一下. 1 ...

  8. JSP下拉选框,级联选择

    前端: <%@ page contentType="text/html;charset=UTF-8" %> <!DOCTYPE html PUBLIC " ...

  9. DBA日常SQL之查询数据库运行状况

    ,) Day, ,),,)) H00, ,),,)) H01, ,),,)) H02, ,),,)) H03, ,),,)) H04, ,),,)) H05, ,),,)) H06, ,),,)) H ...

  10. ajax同步,异步

    传输数据 同步:第一条传过去,对方接收到反馈以后,再传第二条; 异步:第一条传过去,不管对方有没有收到,继续传第二条. ajax(默认异步 async:true) 同步:执行完ajax内部代码,才执行 ...