using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using Microsoft.Win32;
using System.Timers;namespace KJCFTP_Services
{
public class KJCFTP_Service : System.ServiceProcess.ServiceBase
{
/// <summary> 
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Diagnostics.EventLog eventLogKJFTP;
private int m_dtDown;//每天从服务器上down文件的时间
private System.Timers.Timer myTimer;//定时器 public KJCFTP_Service()
{
InitializeComponent();
//写进日志;
if (!System.Diagnostics.EventLog.SourceExists("MySource")) 
{         
System.Diagnostics.EventLog.CreateEventSource(
"MySource","MyNewLog");
}
eventLogKJFTP.Source = "MySource";
eventLogKJFTP.Log = "MyNewLog";

myTimer = new System.Timers.Timer(60*60*1000);
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); } static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new KJCFTP_Service() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
} private void InitializeComponent()
{
this.eventLogKJFTP = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLogKJFTP)).BeginInit();
this.ServiceName = "KJCFTP_Service";
((System.ComponentModel.ISupportInitialize)(this.eventLogKJFTP)).EndInit(); } protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} private void myTimer_Elapsed(object source, ElapsedEventArgs e)
{
if (m_dtDown == System.DateTime.Now.Hour)
{
GetFile();
}
} private  void GetFile()
{
FTPFactory ff = new FTPFactory();
ff.setDebug(true);
ff.setRemoteHost(ReadReg("IPHost"));
ff.setRemoteUser(ReadReg("User"));
ff.setRemotePass(ReadReg("PWD"));
ff.setRemotePort(Convert.ToInt32(ReadReg("Port")));
ff.login();
ff.chdir(ReadReg("Dir")); ff.setBinaryMode(true);
string[] fileNames = ff.getFileList("*.*");
eventLogKJFTP.WriteEntry(fileNames.Length.ToString()+"个文件");
for(int i=0;i < fileNames.Length-1;i++) 
{
eventLogKJFTP.WriteEntry(fileNames[i]+"已经下载!");
ff.download(fileNames[i],"D:/Test" + "/" +fileNames[i]);
}
ff.close();
} protected override void OnStart(string[] args)
{
eventLogKJFTP.WriteEntry("KJCFTP开始运行!");
try
{
dtDown = Convert.ToInt32(ReadReg("DownTime"));
}
catch
{
eventLogKJFTP.WriteEntry("注册表中有关于时间设置的格式不正确!");
}
finally
{}
myTimer.AutoReset = true;
myTimer.Enabled = true;
}
 
protected override void OnStop()
{
eventLogKJFTP.WriteEntry("KJCFTP服务停止!");
myTimer.Enabled = false;
} private int dtDown
{
get{return m_dtDown;}
set{m_dtDown = value;}
} private string ReadReg(string Key)
{
Microsoft.Win32.RegistryKey myKey = Registry.LocalMachine;
RegistryKey KJCFTP = myKey.OpenSubKey("SOFTWARE").OpenSubKey("KJCFTP");
return KJCFTP.GetValue(Key).ToString();
}
}
}

解决方案 »

  1.   

    这是我刚写的,刚刚测试是可以
    关键的地方久下面几句话
    private System.Timers.Timer myTimer;//定时器myTimer = new System.Timers.Timer(60*60*1000);
    myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
    myTimer.AutoReset = true;
    myTimer.Enabled = true;private void myTimer_Elapsed(object source, ElapsedEventArgs e)
    {}
      

  2.   

    用MSTask,也就是计划任务不行么
    为什么要自己写
      

  3.   

    KevinCao(我老婆很小) ,你这个是winform程序吗?是不是需要放到启动里啊?
      

  4.   

    to ht_csc(清风竹韵);我做的是windows服务程序,这个和winform差不多吧,不需要放在启动里,可以设置这个服务自动启动,但我是另外做了个配置程序来配置属性和管理启动
      

  5.   

    直接做windows服务程序,用线程来实现,线程休眠时间设置为20小时。。程序编写完成,再生成安装程序即可。自己试试,不难。