C#实现开发windows服务实现自动从FTP服务器下载文件(自行设置分/时执行)

来源:互联网 发布:python math包 编辑:程序博客网 时间:2024/06/06 01:46

最近在做一个每天定点从FTP自动下载节目.xml并更新到数据库的功能。首先想到用 FileSystemWatcher来监控下载到某个目录中的文件是否发生改变,如果改变就执行相应的操作,然后用timer来设置隔多长时间来下载。后来又想想了。用windwos服务来实现吧。

效果图:


执行的Log日志:

INFO-2016/5/24 0:30:07--日志内容为:0/30/7进行time触发INFO-2016/5/24 1:30:07--日志内容为:1/30/7进行time触发INFO-2016/5/24 2:30:07--日志内容为:2/30/7进行time触发INFO-2016/5/24 3:30:07--日志内容为:3/30/7进行time触发INFO-2016/5/24 4:30:07--日志内容为:4/30/7进行time触发INFO-2016/5/24 5:30:07--日志内容为:5/30/7进行time触发INFO-2016/5/24 6:30:07--日志内容为:6/30/7进行time触发INFO-2016/5/24 7:30:07--日志内容为:7/30/7进行time触发INFO-2016/5/24 7:30:07--日志内容为:TimerEvent 01INFO-2016/5/24 7:30:07--日志内容为:TimerEvent 01 :共获取77个节目列表信息成功。INFO-2016/5/24 7:31:08--日志内容为:服务器与本地节目列表信息进行对比开始。INFO-2016/5/24 7:31:08--日志内容为:得到要更新的节目列表共77个INFO-2016/5/24 7:31:08--日志内容为:FileEvent 01 :BTV-1(高清).xml文件重新下载开始。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 01 :BTV-1(高清).xml文件内容已于2016/05/24 00:01进行changed并重新下载成功。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 01 :BTV-1(高清).xml文件下载后的修改时间开始与服务器修改时间同步开始。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 01 :BTV-1(高清).xml文件下载后的修改时间开始与服务器修改时间同步成功。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 02 :CCTV-1(高清).xml文件重新下载开始。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 02 :CCTV-1(高清).xml文件内容已于2016/05/24 00:01进行changed并重新下载成功。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 02 :CCTV-1(高清).xml文件下载后的修改时间开始与服务器修改时间同步开始。INFO-2016/5/24 7:31:08--日志内容为:FileEvent 02 :CCTV-1(高清).xml文件下载后的修改时间开始与服务器修改时间同步成功。INFO-2016/5/24 7:31:33--日志内容为:FileEvent 77 :黑龙江卫视(高清).xml文件重新下载开始。INFO-2016/5/24 7:31:33--日志内容为:FileEvent 77 :黑龙江卫视(高清).xml文件内容已于2016/05/24 00:01进行changed并重新下载成功。INFO-2016/5/24 7:31:33--日志内容为:FileEvent 77 :黑龙江卫视(高清).xml文件下载后的修改时间开始与服务器修改时间同步开始。INFO-2016/5/24 7:31:33--日志内容为:FileEvent 77 :黑龙江卫视(高清).xml文件下载后的修改时间开始与服务器修改时间同步成功。INFO-2016/5/24 8:31:08--日志内容为:8/31/8进行time触发INFO-2016/5/24 9:31:08--日志内容为:9/31/8进行time触发INFO-2016/5/24 10:31:08--日志内容为:10/31/8进行time触发INFO-2016/5/24 11:31:08--日志内容为:11/31/8进行time触发INFO-2016/5/24 12:31:08--日志内容为:12/31/8进行time触发INFO-2016/5/24 13:31:08--日志内容为:13/31/8进行time触发INFO-2016/5/24 14:31:08--日志内容为:14/31/8进行time触发INFO-2016/5/24 15:31:08--日志内容为:15/31/8进行time触发INFO-2016/5/24 16:31:08--日志内容为:16/31/8进行time触发INFO-2016/5/24 17:31:08--日志内容为:17/31/8进行time触发INFO-2016/5/24 18:31:08--日志内容为:18/31/8进行time触发INFO-2016/5/24 19:31:08--日志内容为:19/31/8进行time触发INFO-2016/5/24 20:31:08--日志内容为:20/31/8进行time触发INFO-2016/5/24 21:31:08--日志内容为:21/31/8进行time触发INFO-2016/5/24 22:31:08--日志内容为:22/31/8进行time触发INFO-2016/5/24 23:31:08--日志内容为:23/31/8进行time触发

实现代码:

下载 ftplib.dll 然后项目中参照引用

using FtpLib;using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.ServiceProcess;using System.Threading;namespace WindowsService1{public partial class Service1 : ServiceBase{private int _countFileChangeEvent = 0, _countTimerEvent = 0;private Thread ThreadDownLoad;public Service1(){InitializeComponent();}//http://blog.csdn.net/hwt0101/article/details/8514291//http://www.cnblogs.com/mywebname/articles/1244745.html//http://www.cnblogs.com/jzywh/archive/2008/07/23/filesystemwatcher.html//http://www.cnblogs.com/hfzsjz/archive/2011/01/07/1929898.html/// <summary>/// 服务启动的操作/// </summary>/// <param name="args"></param>protected override void OnStart(string[] args){try{EventLog.WriteEntry("监控服务器与本地节目列表信息线程任务开始");//在系统事件查看器里的应用程序事件里来源的描述  WriteInLog("监控服务器与本地节目列表信息线程任务开始", false);System.Timers.Timer t = new System.Timers.Timer();//t.Interval =  60000; t.Interval = 60 * 60 * 1000;      t.Elapsed += new System.Timers.ElapsedEventHandler(BeginDowLoad);//到达时间的时候执行事件;   t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);   t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;    t.Start();}catch (Exception ex){System.Diagnostics.Trace.Write(ex.Message);throw ex;}}/// <summary>  /// 定时检查,并执行方法  /// </summary>  /// <param name="source"></param>  /// <param name="e"></param>  public void BeginDowLoad(object source, System.Timers.ElapsedEventArgs e){int intMinute = e.SignalTime.Minute;int intSecond = e.SignalTime.Second;int intHours = e.SignalTime.Hour;//设置 每天的07:30开始执行程序 WriteInLog(intHours+"/"+ intMinute + "/"+ intSecond + "进行time触发", false);if (intHours == 07 ) ///定时设置,判断分时秒    && intMinute == 10{try{System.Timers.Timer tt = (System.Timers.Timer)source;tt.Enabled = false;DownLoadTvListInfo();tt.Enabled = true;}catch (Exception err){WriteInLog(err.Message, false);}}}public List<ChannelTvListInfo> listFTPFiles(string FTPAddress, string username, string password){List<ChannelTvListInfo> listinfo = new List<ChannelTvListInfo>();using (FtpConnection ftp = new FtpConnection(FTPAddress, username, password)){ftp.Open();ftp.Login();foreach (var file in ftp.GetFiles("/")){listinfo.Add(new ChannelTvListInfo{TVName = file.Name,LastWriteTime = Convert.ToDateTime(file.LastWriteTime).ToString("yyyy/MM/dd HH:mm")});}ftp.Dispose();ftp.Close();}return listinfo;}/// <summary>/// 服务停止的操作/// </summary>protected override void OnStop(){try{ThreadDownLoad.Abort();WriteInLog("监控服务器与本地节目列表信息线程任务停止", false);System.Diagnostics.Trace.Write("监控服务器与本地节目列表信息线程任务停止");EventLog.WriteEntry("监控服务器与本地节目列表信息线程任务停止");}catch (Exception ex){System.Diagnostics.Trace.Write(ex.Message);}}private List<ChannelTvListInfo> lstNewTvInfo, lstOldTvInfo = new List<ChannelTvListInfo>();private void DownLoadTvListInfo(){_countTimerEvent++;WriteInLog(string.Format("TimerEvent {0}", _countTimerEvent.ToString("#00")), false);lstNewTvInfo = listFTPFiles("222.206.159.xx", "xx", "xx");WriteInLog(string.Format("TimerEvent {0} :共获取{1}个节目列表信息成功。", _countTimerEvent.ToString("#00"),lstNewTvInfo.Count), false);lstOldTvInfo = new List<ChannelTvListInfo>();DirectoryInfo TheFolder = new DirectoryInfo(@"D:\Hello\UpLoadImg\ChannelTvXML");foreach (FileInfo NextFile in TheFolder.GetFileSystemInfos()){lstOldTvInfo.Add(new ChannelTvListInfo { TVName = NextFile.Name, LastWriteTime = NextFile.LastWriteTime.ToString("yyyy/MM/dd HH:mm") });}      Thread.Sleep(60000);ThreadDownLoad = new Thread(new ThreadStart(Test));ThreadDownLoad.Start();WriteInLog("服务器与本地节目列表信息进行对比开始。", false);System.Diagnostics.Trace.Write("线程任务开始");}public void Test(){try{var result = lstNewTvInfo.Except(lstOldTvInfo, new ProductComparer()).ToList();WriteInLog("得到要更新的节目列表共"+ result.Count+"个", false);if (result.Count > 0){foreach (var item in result){_countFileChangeEvent++;WriteInLog(string.Format("FileEvent {0} :{1}文件重新下载开始。", _countFileChangeEvent.ToString("#00"),item.TVName), false);new FtpHelper().DownloadFtpFile("xx", "xx", "222.206.159.xx", @"D:\Hello\UpLoadImg\ChannelTvXML", item.TVName);WriteInLog(string.Format("FileEvent {0} :{1}文件内容已于{2}进行{3}", _countFileChangeEvent.ToString("#00"),                                item.TVName, item.LastWriteTime, "changed并重新下载成功。"), false);WriteInLog(string.Format("FileEvent {0} :{1}文件下载后的修改时间开始与服务器修改时间同步开始。", _countFileChangeEvent.ToString("#00"),item.TVName), false);File.SetLastWriteTime(@"D:\Hello\UpLoadImg\ChannelTvXML\" + item.TVName,Convert.ToDateTime(new FtpHelper().GetDateTimestamp("222.206.159.xx", item.TVName, "xx", "quanmeiti").xx("yyyy/MM/dd hh:mm tt")));WriteInLog(string.Format("FileEvent {0} :{1}文件下载后的修改时间开始与服务器修改时间同步成功。", _countFileChangeEvent.ToString("#00"),item.TVName), false);}}else{WriteInLog("暂无服务器电视节目列表更新", false);}}catch { }Thread.Sleep(60000);}/// <summary>/// 写入文件操作/// </summary>/// <param name="msg">写入内容</param>/// <param name="IsAutoDelete">是否删除</param>private void WriteInLog(string msg, bool IsAutoDelete){try{string logFileName = @"D:\Hello\UpLoadImg\ChannelLog\DownTvList_" + DateTime.Now.ToString("yyyyMMdd") + "_log.txt" + ""; // 文件路径FileInfo fileinfo = new FileInfo(logFileName);if (IsAutoDelete){if (fileinfo.Exists && fileinfo.Length >= 1024){fileinfo.Delete();}}using (FileStream fs = fileinfo.OpenWrite()){StreamWriter sw = new StreamWriter(fs);sw.BaseStream.Seek(0, SeekOrigin.End);sw.Write("INFO-" + DateTime.Now.ToString() + "--日志内容为:" + msg + "\r\n");sw.Flush();sw.Close();}}catch (Exception ex){ex.ToString();}}}}

实现从FTP下载文件方法

/// <summary>///从ftp服务器上下载文件的功能/// </summary>/// <param name="userId"></param>/// <param name="pwd"></param>/// <param name="ftpUrl">ftp地址</param>/// <param name="filePath"></param>/// <param name="fileName"></param>public void DownloadFtpFile(string userId, string pwd, string ftpUrl, string filePath, string fileName){FtpWebRequest reqFTP = null;FtpWebResponse response = null;try{String onlyFileName = Path.GetFileName(fileName);string downFileName = filePath + "\\" + onlyFileName;string url = "ftp://" + ftpUrl + "/" + fileName;if (File.Exists(downFileName)){DeleteDir(downFileName);}FileStream outputStream = new FileStream(downFileName, FileMode.Create);reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));reqFTP.Credentials = new NetworkCredential(userId, pwd);reqFTP.UseBinary = true;reqFTP.UsePassive = true;reqFTP.KeepAlive = true;reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream();long cl = response.ContentLength;int bufferSize = 2048;int readCount;byte[] buffer = new byte[bufferSize];readCount = ftpStream.Read(buffer, 0, bufferSize);while (readCount > 0){outputStream.Write(buffer, 0, readCount);readCount = ftpStream.Read(buffer, 0, bufferSize);}ftpStream.Close();outputStream.Close();response.Close();}catch (Exception ex){throw ex;}}


设置widnwos服务就不多介绍了。如下图:






1 0