Windows注册系统服务

7 篇文章 0 订阅
7 篇文章 0 订阅

需求:在做程序接口需要托管至服务开机自启动、监听服务等功能的。

入口函数:

using System;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Windows.Forms;
 
namespace MailServerService {
    /// <summary>
    /// Application main class.
    /// </summary>
    public class Program {
        #region static method Main
 
        /// <summary>
        /// Application main entry point.
        /// </summary>
        /// <param name="args">Command line argumnets.</param>
        public static void Main(string[] args) {
            try {
                if (args.Length > 0 && args[0].ToLower() == "-install") {
                    ManagedInstallerClass.InstallHelper(new string[] { "MailServerService.exe" });
 
                    ServiceController c = new ServiceController("MiFanMailServer");
                    c.Start();
                } else if (args.Length > 0 && args[0].ToLower() == "-uninstall") {
                    ManagedInstallerClass.InstallHelper(new string[] { "/u", "MailServerService.exe" });
                } else {
                    ServiceBase[] servicesToRun = new ServiceBase[] { new MailServer_Service() };
                    ServiceBase.Run(servicesToRun);
                }
            } catch (Exception e) {
                MessageBox.Show("错误: " + e.ToString()+"需要以管理员身份运行!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        #endregion
    }
}


using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
 
namespace MailServerService {
	
	[RunInstaller(true)]
	public class MailServer_Service_Installer : Installer
	{
		
		private System.ComponentModel.Container components = null;
 
		private ServiceInstaller serviceInstaller;
		private ServiceProcessInstaller processInstaller;
 
		public MailServer_Service_Installer()
		{
			processInstaller = new ServiceProcessInstaller();
			serviceInstaller = new ServiceInstaller();
			processInstaller.Account = ServiceAccount.LocalSystem;
			serviceInstaller.StartType = ServiceStartMode.Automatic;            
			serviceInstaller.ServiceName = "MiFanMailServer";
                        serviceInstaller.Description = "邮件服务器";
			Installers.Add(serviceInstaller);
			Installers.Add(processInstaller);
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值