我可以通过身份验证,但我不确定如何读取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();发布于 2014-06-16 11:24:23
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);发布于 2014-06-16 10:57:56
看看这个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发布于 2015-04-28 15:16:18
这是你的ftp://ftp.atozfdc.com.au/data.csv文件路径。你还在找什么?如果您想要读取该文件,则使用
StreamReader reader = new StreamReader(responseStream);
string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)https://stackoverflow.com/questions/24236004
复制相似问题