【问题标题】:How to read a CSV file from FTP using C#如何使用 C# 从 FTP 读取 CSV 文件
【发布时间】:2014-06-16 02:38:51
【问题描述】:

我能够通过身份验证,但我不确定如何读取 CSV 文件。通常从我正在阅读的硬盘驱动器链接。

string[] allLines = System.IO.File.ReadAllLines(@"D:\CSV\data.csv");

但是我如何从 ftp 读取。我的 Ftp 路径是这样的ftp://ftp.atozfdc.com.au/data.csv 这是我通过 ftp 身份验证的代码。

String ftpserver = "ftp://ftp.atozfdc.com.au/data.csv";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
reqFTP.UsePassive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

【问题讨论】:

  • 您是否需要代理凭据才能连接到您的 ftp?尝试此代码时是否有任何异常?
  • 我没有收到任何错误我在问如何从我的本地计算机获取文件路径,例如我通过我的文件路径这样 System.IO.File.ReadAllLines(@"D:\CSV \data.csv");

标签: c# .net csv ftp c#-3.0


【解决方案1】:
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
//use the response like below
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

【讨论】:

    【解决方案2】:

    看看这个link


    TextFieldParser 位于 Microsoft.VisualBasic.FileIO 命名空间中。因此您需要添加对Microsoft.VisualBasic.dll的引用

    String path = @"D:\CSV\data.csv";
    
    using (TextFieldParser parser = new TextFieldParser(path))
    {
        parser.SetDelimiters(new string[] { "," });
        parser.HasFieldsEnclosedInQuotes = true;
    
        // if you want to skip over header line., uncomment line below
        // parser.ReadLine();
    
        while (!parser.EndOfData)
        {
            string[] fields = parser.ReadFields();
            column1 = fields[0];
            column2 = fields[1];
            column3 = int.Parse(fields[2]);
            column4 = double.Parse(fields[3]);
        }
    }
    

    我建议您将文件下载到临时位置,然后使用临时文件路径解析 CSV。

    但如果您不想创建临时文件,请尝试使用ResponseStream

    例子:

    String ftpserver = "ftp://ftp.atozfdc.com.au/data.csv";
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
    reqFTP.UsePassive = false;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential("username", "password");
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    
    
    Stream responseStream = response.GetResponseStream();
    // use the stream to read file from remote location
    
    using (TextFieldParser parser = new TextFieldParser(responseStream))
    {
        // usual csv reader implementation
    }
    
    responseStream.Close();
    response.Close(); //Closes the connection to the server
    

    【讨论】:

    • 非常感谢我可以解析 CSV 文件。这不是我要说的问题,我现在正在从本地计算机读取 CSV,它工作正常,但我想从 ftp 服务器读取文件,我已经在 ftp 上上传了我的 csv,我能够通过所有身份验证但是我不确定如何获取 ftp 文件路径。这样我就可以替换 @"D:\CSV\data.csv";此行到 ftp 服务器 csv
    【解决方案3】:

    ftp://ftp.atozfdc.com.au/data.csv 这是您的 ftp 文件路径。你还在寻找什么?如果你想读取文件然后使用

    StreamReader reader = new StreamReader(responseStream);
    string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2023-03-06
      • 2018-05-24
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多