C# 如何从FTP位置下载最新文件

C# 如何从FTP位置下载最新文件,c#,sharepoint-2013,C#,Sharepoint 2013,如何从FTP位置下载最新文件?我有很多文件在FTP位置,今天有人在FTP位置放置了3个文件。我想用c#.net下载三个最新的文件。试试下面 using System; using System.Collections.Generic; using System.Web; using System.Net; using System.IO; public static int DownloadFtp(string filePath, string fileName, string ftpServ

如何从FTP位置下载最新文件?我有很多文件在FTP位置,今天有人在FTP位置放置了3个文件。我想用c#.net下载三个最新的文件。

试试下面

using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.IO;

public static int DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
    FtpWebRequest reqFTP;
    try
    {
        //filePath = < <The full path where the file is to be created.>>, 
        //fileName = < <Name of the file to be created(Need not be the name of the file on FTP server).>> 
        FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.KeepAlive = false;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        FtpWebResponse 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();
        return 0;
    }
    catch (Exception ex)
    {
        // Logging.WriteError(ex.Message + ex.StackTrace);
        // System.Windows.Forms.MessageBox.Show(ex.Message);
        return -2;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Web;
Net系统;
使用System.IO;
公共静态int DownloadFtp(字符串文件路径、字符串文件名、字符串ftpServerIP、字符串ftpUserID、字符串ftpPassword)
{
ftpWebRequestReqftp;
尝试
{
//文件路径=<>,
//文件名=<>
FileStream outputStream=newfilestream(filePath+“\\”+文件名,FileMode.Create);
reqFTP=(FtpWebRequest)FtpWebRequest.Create(新Uri(“ftp://“+ftpServerIP+”/“+fileName));
reqFTP.Method=WebRequestMethods.Ftp.DownloadFile;
reqFTP.useBarbinary=true;
reqFTP.KeepAlive=false;
reqFTP.Credentials=新的网络凭证(ftpUserID、ftpPassword);
FtpWebResponse响应=(FtpWebResponse)reqFTP.GetResponse();
流ftpStream=response.GetResponseStream();
long cl=响应。内容长度;
int bufferSize=2048;
整数读取计数;
字节[]缓冲区=新字节[bufferSize];
readCount=ftpStream.Read(缓冲区,0,缓冲区大小);
而(读取计数>0)
{
写入(缓冲区,0,读取计数);
readCount=ftpStream.Read(缓冲区,0,缓冲区大小);
}
ftpStream.Close();
outputStream.Close();
response.Close();
返回0;
}
捕获(例外情况除外)
{
//Logging.WriteError(例如Message+ex.StackTrace);
//System.Windows.Forms.MessageBox.Show(例如Message);
返回-2;
}
}

您能展示您目前的尝试吗?这是如何显示“最新文件”的?(我假设OP希望将最新文件上传到FTP位置)