2014-06-16 47 views
4

我能够通过身份验证,但我不知道如何读取CSV文件。通常从硬盘链接我正在阅读这样的。如何使用C#从FTP读取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(); 
+0

你需要代理证书连接到你的FTP?当您尝试此代码时有任何异常? – Damith

+0

我没有收到任何错误,我问如何从我的本地计算机获取文件路径,例如通过我的文件路径像这样System.IO.File.ReadAllLines(@“D:\ CSV \ data.csv”); – user3718016

回答

6
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); 
3

看一看这个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 
+0

非常感谢我可以解析一个CSV文件。这不是我的问题,我现在说我正在从我的本地计算机读取CSV并且它的工作正常,但是我想从ftp服务器读取一个文件,并且我已经将ftp上的csv上传了,并且能够通过所有的身份验证,但是我不知道如何获得一个ftp文件路径。这样我就可以替换@“D:\ CSV \ data.csv”;这行到ftp服务器csv – user3718016

+0

@ user3718016回答更新! –

0

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

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