Windows Service

作者:互联网   出处:控件中国网   2014-11-05 19:21:26   阅读:1

Windows Service

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);
            }
        }
    }
}

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat