Read file from FTP to memory in C#

前端 未结 9 1486
长发绾君心
长发绾君心 2020-12-03 05:15

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()
{
         


        
9条回答
  •  春和景丽
    2020-12-03 05:52

    WebClient request = new WebClient();
    string url = "ftp://ftp.microsoft.com/developr/fortran/" +"README.TXT";
    request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com");
    request.Proxy = null;
    
    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.
    }
    

提交回复
热议问题