Windows Service

本文介绍了一个使用C#实现的Windows服务安装程序(ProjectInstaller)及其服务主体(AccountService)的实现细节。该服务能够周期性地执行账户清理任务,并通过配置文件自定义执行间隔和服务描述等参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ProjectInstaller.cs    安装文件

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration.Install;

using System.Linq;

 

 

namespace AccountService

{

    [RunInstaller(true)]

    public partial class ProjectInstaller : Installer

    {

        public ProjectInstaller()

        {

            InitializeComponent();

        }

 

        //This code should be inserted into your ProjectInstaller class' code

 

        public override void Install(IDictionary stateServer)

        {

            Microsoft.Win32.RegistryKey system,

                //HKEY_LOCAL_MACHINE\Services\CurrentControlSet

              currentControlSet,

                //...\Services

              services,

                //...\<Service Name>

              Scaning,

                //...\Parameters - this is where you can put service-specific configuration

              config;

 

            try

            {

                //Let the project installer do its job

                base.Install(stateServer);

 

                //Open the HKEY_LOCAL_MACHINE\SYSTEM key

                system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");

                //Open CurrentControlSet

                currentControlSet = system.OpenSubKey("CurrentControlSet");

                //Go to the services key

                services = currentControlSet.OpenSubKey("Services");

                //Open the key for your service, and allow writing

                Scaning = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);

                //Add your service's description as a REG_SZ value named "Description"

                Scaning.SetValue("Description", System.Configuration.ConfigurationManager.AppSettings["serviceDescription"].ToString());

                //(Optional) Add some custom information your service will use...

                config = Scaning.CreateSubKey("Parameters");

            }

            catch (Exception e)

            {

                Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());

            }

        }

 

        public override void Uninstall(IDictionary stateServer)

        {

            Microsoft.Win32.RegistryKey system,

              currentControlSet,

              services,

              service;

 

            try

            {

                //Drill down to the service key and open it with write permission

                system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");

                currentControlSet = system.OpenSubKey("CurrentControlSet");

                services = currentControlSet.OpenSubKey("Services");

                service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);

                //Delete any keys you created during installation (or that your service created)

                service.DeleteSubKeyTree("Parameters");

                //...

            }

            catch (Exception e)

            {

                Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());

            }

            finally

            {

                //Let the project installer do its job

                base.Uninstall(stateServer);

            }

        }

 

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)

        {

 

        }

 

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)

        {

 

        }

    }

}

 

 

 

Windows Service.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Linq;

using System.ServiceProcess;

using System.Text;

using ComLib;

using Com.Auo.SSP3g;

using log4net;

 

namespace AccountService

{

    public partial class AccountService : ServiceBase

    {

        public AccountService()

        {

            InitializeComponent();

        }

 

        protected override void OnStart(string[] args)

        {

            timerAccount.Interval =double.Parse(System.Configuration.ConfigurationManager.AppSettings["accountServiceTime"].ToString());

            timerAccount.Elapsed += new System.Timers.ElapsedEventHandler(RemoveAccount_Elapsed);

            timerAccount.Start();

        }

 

        protected override void OnStop()

        {

            timerAccount.Stop();

        }

 

        void RemoveAccount_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            try

            {

                string actionTime = System.Configuration.ConfigurationManager.AppSettings["actionTime"].ToString();

                string currentTime = DateTime.Now.ToShortTimeString();

                string judgeDate = System.Configuration.ConfigurationManager.AppSettings["judgeDate"].ToString();

                if (actionTime == currentTime)

                {

                    AccountManager accountManager = apc.GetManager<AccountManager>();

                    accountManager.DeleteInvalidAccount(judgeDate);

                }

            }

            catch (Exception ex)

            {

                Com.Auo.SSP3g.Logs.Logger.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()).Error(ex.Message);

            }

        }

    }

}

转载于:https://www.cnblogs.com/pinnasky/archive/2011/03/15/1985293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值