windows service

来源:互联网 发布:洛奇英雄传优化补丁 编辑:程序博客网 时间:2024/06/02 14:33

一、新建工程


二、添加Timer

Service1.Designer.cs文件中

private void InitializeComponent()

        {

            components = new System.ComponentModel.Container();

            this.ServiceName = "Service1";

            _timer = new Timer();

            // 每隔5分钟执行

            this._timer.Interval = 5000;// 5 * 60 * 1000;

            // 设置timer可以激发Elapsed事件

            this._timer.Enabled = true;

            // 开始

            this._timer.Start();

 

            this._timer.Elapsed += new System.Timers.ElapsedEventHandler(this._timer_Elapsed);           

        }

 

        #endregion

 

        #region Timer

        public Timer _timer;

 

        public bool _Scan(bool _judge)

        {

            //TODO

            string[] strFile = System.IO.Directory.GetFiles("E:/TestBase/");

            if (strFile != null && strFile.Length > 0)

                return true;

            else

                return false;

        }

        public void _DO_Something()

        {

            //TODO

            string[] strFile = System.IO.Directory.GetFiles("E:/TestBase/");

            foreach (string name in strFile)

            {

                string[] tmp = name.Split('/');

                string aimName = "E:/TestAim/" + tmp[2];

                if (!System.IO.File.Exists(aimName))

                {

                    System.IO.File.Copy(name, aimName);

                    System.IO.File.Delete(name);

                }

            }

        }

 

        private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            _timer.Interval = 1000;

            _timer.Enabled = false;

            //if (_Scan(true) == true)

            //{

                _DO_Something();

            //}

            _timer.Enabled = true;

        }

 

        #endregion

三、添加安装类ProjectInstaller

 yazz

四、属性设置

Service1属性

vs2005切换到属性浏览页面,Service1.cs会有以下属性:
Autolog              
是否自动写入系统的日志文件
CanHandlePowerEvent  
服务时候接受电源事件
CanPauseAndContinue  
服务是否接受暂停或继续运行的请求
CanShutdown          
服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程
CanStop              
服务是否接受停止运行的请求
ServiceName          
服务名

ProjectInstaller属性

 yaazz

yaazz

ServiceName:安装后Service的名字

StartType:服务启动类型

五、安装、卸载

yaazz

 

 

原创粉丝点击