从FTP服务器下载新的和修改的文件 [英] Download new and modified files from an FTP server

查看:182
本文介绍了从FTP服务器下载新的和修改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取FTP服务器上的文件列表,然后逐个检查本地系统上是否存在该文件,以及它是否比较修改的日期以及ftp文件是否较新下载它。

  private void btnGo_Click(object sender,EventArgs e)
{
string [] files = GetFileList( );
foreach(文件中的字符串文件)
{
if(file.Length> = 5)
{
string uri =ftp://+ ftpServerIP +/+ remoteDirectory +/+ file;
Uri serverUri = new Uri(uri);

CheckFile(file);
}
}
this.Close();


public string [] GetFileList()
{
string [] downloadFiles;
StringBuilder结果=新的StringBuilder();
WebResponse response = null;
StreamReader reader = null;

try
{
FtpWebRequest reqFTP =(FtpWebRequest)FtpWebRequest.Create(new Uri(ftp://+ ftpServerIP +/+ remoteDirectory));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID,ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDe​​tails;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();
while(line!= null)
{
result.Append(line);
result.Append(\\\
);
line = reader.ReadLine();
}
result.Remove(result.ToString()。LastIndexOf('\\\
'),1);
return result.ToString()。Split('\\\
');
}
catch
{
if(reader!= null)
{
reader.Close();

if(response!= null)
{
response.Close();
}
downloadFiles = null;
返回downloadFiles;



private void CheckFile(string file)
{
string dFile = file;
string [] splitDownloadFile = Regex.Split(dFile,);
字符串fSize = splitDownloadFile [13];
字符串fMonth = splitDownloadFile [14];
字符串fDate = splitDownloadFile [15];
字符串fTime = splitDownloadFile [16];
string fName = splitDownloadFile [17];


string dateModified = fDate +/+ fMonth +/+ fYear;

DateTime lastModifiedDF = Convert.ToDateTime(dateModified);

string [] filePaths = Directory.GetFiles(localDirectory);

//如果filePaths中有一个文件与服务器上的文件相同,则比较它们,然后在服务器上的文件更新时下载
foreach(filePaths中的字符串ff)
{

string [] splitFile = Regex.Split(ff,@\\);

字符串fileName = splitFile [2];
FileInfo fouFile = new FileInfo(ff);
DateTime lastChangedFF = fouFile.LastAccessTime;
if(lastModifiedDF> lastChangedFF)下载(fileName);






在检查文件方法中,对于每个文件(它们是.exe文件)当我分割字符串时,我不断得到不同的结果,即一个文件的文件名是在第18列,另一个是在16等。我也不能总是得到文件的年份部分。

解决方案

选项A:我建议您使用更高级别的FTP客户端库来处理其中的一些细节,可能的选择是:



选项B:为了更直接回答您的问题,我认为问题出现在以下这一行: p>

  string [] splitDownloadFile = Regex.Split(dFile,) ; 

似乎FTP服务器使用空格来对齐文件名。为了解决这个问题,我们想调整regex来消除字段之间的所有空白:

  string [] splitDownloadFile = Regex。分割(dFile,\s +); 

...其中\s代表任何空格字符(通常是制表符或空格),+意味着它左边的一个或多个东西。这不会处理边缘情况,例如文件名中带有空格。


I'm trying to get a list of the files on an FTP server, then one by one check if that file exists on the local system and if it does compare the dates modified and if the ftp file is newer download it.

private void btnGo_Click(object sender, EventArgs e)
{
    string[] files = GetFileList();
    foreach (string file in files)
    {
        if (file.Length >= 5)
        {
            string uri = "ftp://" + ftpServerIP + "/" + remoteDirectory + "/" + file;
            Uri serverUri = new Uri(uri);

            CheckFile(file);
        }
    }
    this.Close();
}

public string[] GetFileList()
{
    string[] downloadFiles;
    StringBuilder result = new StringBuilder();
    WebResponse response = null;
    StreamReader reader = null;

    try
    {
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + remoteDirectory));
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        reqFTP.Proxy = null;
        reqFTP.KeepAlive = false;
        reqFTP.UsePassive = false;
        response = reqFTP.GetResponse();
        reader = new StreamReader(response.GetResponseStream());

        string line = reader.ReadLine();
        while (line != null)
        {
            result.Append(line);
            result.Append("\n");
            line = reader.ReadLine();
        }
        result.Remove(result.ToString().LastIndexOf('\n'), 1);
        return result.ToString().Split('\n');
    }
    catch
    {
        if (reader != null)
        {
            reader.Close();
        }
        if (response != null)
        {
            response.Close();
        }
        downloadFiles = null;
        return downloadFiles;
    }
}

private void CheckFile(string file)
{
    string dFile = file;
    string[] splitDownloadFile = Regex.Split(dFile, " ");
    string fSize = splitDownloadFile[13];
    string fMonth = splitDownloadFile[14];
    string fDate = splitDownloadFile[15];
    string fTime = splitDownloadFile[16];
    string fName = splitDownloadFile[17];


    string dateModified = fDate + "/" + fMonth+ "/" + fYear;

    DateTime lastModifiedDF = Convert.ToDateTime(dateModified);

    string[] filePaths = Directory.GetFiles(localDirectory);

    // if there is a file in filePaths that is the same as on the server compare them and then download if file on server is newer
    foreach (string ff in filePaths)
    {

        string[] splitFile = Regex.Split(ff, @"\\");

        string fileName = splitFile[2];
        FileInfo fouFile = new FileInfo(ff);
        DateTime lastChangedFF = fouFile.LastAccessTime;
        if (lastModifiedDF > lastChangedFF) Download(fileName);
    }
}

In the check file method, for each file (they are .exe files) I keep getting different results when I split the string i.e. for one file the file name was at column 18 another it was at 16 etc. I also can't always get the year portion of the file.

解决方案

Option A: I'd recommend that you use a higher-level FTP client library that handles some of these details for you, a few likely options are:

Option B: To answer your question more directly, I think the issue is with this line:

string[] splitDownloadFile = Regex.Split(dFile, " ");

It seems like the FTP server is using spaces to right-align the filenames. To deal with that, we want to adjust the regex to consume all whitespace between the fields:

string[] splitDownloadFile = Regex.Split(dFile, "\s+");

...where \s stands for any whitespace character (usually tabs or spaces), and + means one or more of the thing to the left of it. This will not handle edge cases, such as file names with spaces in them.

这篇关于从FTP服务器下载新的和修改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆