一、新建工程
二、添加 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
四、属性设置
Service1 属性
将vs2005
切换到属性浏览页面,Service1.cs
会有以下属性:
Autolog
是否自动写入系统的日志文件
CanHandlePowerEvent
服务时候接受电源事件
CanPauseAndContinue
服务是否接受暂停或继续运行的请求
CanShutdown
服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown
过程
CanStop
服务是否接受停止运行的请求
ServiceName
服务名
ProjectInstaller 属性
ServiceName :安装后 Service 的名字
StartType :服务启动类型
五、安装、卸载