Read file from FTP to memory in C#

2019-01-12 03:05发布

I want to read a file from a FTP server without downloading it to a local file. I wrote a function but it does not work:

private string GetServerVersion()
{
    WebClient request = new WebClient();

    string url = FtpPath + FileName;
    string version = "";

    request.Credentials = new NetworkCredential(ftp_user, ftp_pas);

    try
    {
        byte[] newFileData = request.DownloadData(new Uri(FtpPath)+FileName);
        string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
    }
    catch (WebException e)
    {
    }
    return version;
}

9条回答
SAY GOODBYE
2楼-- · 2019-01-12 03:48

Here's a simple working example using your code to grab a file from the Microsoft public FTP servers:

WebClient request = new WebClient();
string url = "ftp://ftp.microsoft.com/developr/fortran/" +"README.TXT";
request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com");

try
{
  byte[] newFileData = request.DownloadData(url);
  string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
  Console.WriteLine(fileString);
}
catch (WebException e)
{
  // Do something such as log error, but this is based on OP's original code
  // so for now we do nothing.
}

I reckon you're missing a slash on this line here in your code:

string url = FtpPath + FileName;

Perhaps between FtpPath and FileName ?

查看更多
唯我独甜
3楼-- · 2019-01-12 03:49

I know this is an old question, but I thought I'd suggest using path.combine for the next guy reading this. Helps clean up these kinds of issues.

        string url = Path.Combine("ftp://ftp.microsoft.com/developr/fortran", "README.TXT");
查看更多
可以哭但决不认输i
4楼-- · 2019-01-12 03:53

We can use below method to get the file attribute from ftp without downloading the file.This is working fine for me.

    public DataTable getFileListFTP(string serverIP,string userID,string Password)
    {
        DataTable dt = new DataTable();
        string[] fileListArr;
        string fileName = string.Empty;
        long fileSize = 0;
       // DateTime creationDate;
        string creationDate;

        FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(serverIP);
        Request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        Request.Credentials = new NetworkCredential(userID,Password);
        FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
        Stream ResponseStream = Response.GetResponseStream();
        StreamReader Reader = new StreamReader(ResponseStream);
        dt.Columns.Add("FileName", typeof(String));
        dt.Columns.Add("FileSize", typeof(String));
        dt.Columns.Add("CreationDate", typeof(DateTime));
        //CultureInfo c = new CultureInfo("ES-ES");
        while (!Reader.EndOfStream)//Read file name   
        {
            fileListArr = Convert.ToString(Reader.ReadLine()).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            fileSize = long.Parse(fileListArr[4]);
            creationDate = fileListArr[6] + " " + fileListArr[5] + " " + fileListArr[7];
            //creationDate =Convert.ToDateTime(fileListArr[6] + " " + fileListArr[5] + " " + fileListArr[7], c).ToString("dd/MMM/yyyy", DateTimeFormatInfo.InvariantInfo);
            fileName = Convert.ToString(fileListArr[8]);

            DataRow drow = dt.NewRow();
            drow["FileName"] = fileName;
            drow["FileSize"] = fileName;
            drow["CreationDate"] = creationDate;
            dt.Rows.Add(drow);
        }
        Response.Close();
        ResponseStream.Close();
        Reader.Close();
        return dt;
    }
查看更多
登录 后发表回答