首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C#从FTP中读取CSV文件

问如何使用C#从FTP中读取CSV文件
EN

Stack Overflow用户
提问于 2014-06-16 10:38:52
回答 3查看 11.5K关注 0票数 6

我可以通过身份验证,但我不确定如何读取CSV文件。链接通常从硬盘驱动器,我是这样读的。

代码语言:javascript
复制
string[] allLines = System.IO.File.ReadAllLines(@"D:\CSV\data.csv");

但是我怎么才能从ftp中读取。我的Ftp路径类似于ftp://ftp.atozfdc.com.au/data.csv,这里是我通过ftp身份验证的代码。

代码语言:javascript
复制
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();
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-16 11:24:23

代码语言:javascript
复制
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);
票数 10
EN

Stack Overflow用户

发布于 2014-06-16 10:57:56

看看这个link

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

代码语言:javascript
复制
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

示例:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2015-04-28 15:16:18

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

代码语言:javascript
复制
StreamReader reader = new StreamReader(responseStream);
string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24236004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档