windows service

一、新建工程


二、添加 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

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值