Windows Service 创建与安装

使用Vs Windows Service模板可以很方便地开发Windows 服务。在Service文件 的设计界面,右键点击Add Installer,可以添加一个ProjectInstaller类, 然后设定服务的运行账号,名称等。


        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
            this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
            // 
            // serviceProcessInstaller1
            // 
            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
            this.serviceProcessInstaller1.Password = null;
            this.serviceProcessInstaller1.Username = null;
            // 
            // serviceInstaller1
            // 
            this.serviceInstaller1.Description = "Sample Service Description";
            this.serviceInstaller1.DisplayName = "Sample Service";
            this.serviceInstaller1.ServiceName = "SampleService";
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller1,
            this.serviceInstaller1});

        }

        #endregion

        private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
        private System.ServiceProcess.ServiceInstaller serviceInstaller1;

开发完毕后,可以使用.Net Framework自带的InstallUtil.exe安装服务。

installutil [/u[ninstall]] [options] assembly [[options] assembly] …

 

如果想在安装的时候指定服务名,或者想在同一台机器上安装多份服务,可以采用下面的方式:

	        #region Component Designer generated code
	
	        /// <summary>
	        /// Required method for Designer support - do not modify
	        /// the contents of this method with the code editor.
	        /// </summary>
	        private void InitializeComponent()
	        {
	            components = new System.ComponentModel.Container();
	        }
	
	        #endregion

然后使用下面的命令安装:

sc create <servicename> binpath="<pathtobinaryexecutable>" [option1] [option2] [optionN]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值