在解析和读取FTP文件之前处理C#流 [英] C# Stream disposed before ending parsing and reading FTP File
问题描述
我正在从FTP服务器读取CSV文件并在运行中解析它。然而,我偶然发现了正在读取的文件中间的 System.ObjectDisposedException 。
我知道理所当然正在读取文件,因为我的控制台已经给我输出了。
我在网上找到的大多数答案都是关于在阅读完成之前关闭流/解析器的人,但在代码中看不到这样的错误。
以下是有问题的方法:
public static void readCSVfromFTP字符串ftpFileAddress)
{
//设置FTP请求
FtpWebRequest ftpRequest =(FtpWebRequest)FtpWebRequest.Create(ftpFileAddress);
ftpRequest.Credentials = new NetworkCredential(ftpUsername,ftpPassword);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response =(FtpWebResponse)ftpRequest.GetResponse();
//设置响应流&编码
流responseStream = response.GetResponseStream();
//文本解析器
using(TextFieldParser parser = new TextFieldParser(responseStream))
{
//文本解析器设置
parser.SetDelimiters(new string [] {,});
parser.HasFieldsEnclosedInQuotes = false;
//文本标记逻辑&文件解析
((!parser.EndOfData)
{
//定义列
string [] fields = parser.ReadFields();
// int eanNumber = int.Parse(fields [0]);
// int catalogueNumber = int.Parse(fields [1]);
// double quantity = double.Parse(fields [2]);
// DateTime availibilityDate = Convert.ToDateTime(fields [3]);
Console.WriteLine(fields [0] ++ fields [1]);
}
}
//清理并关闭流
responseStream.Close();
response.Close();
}
我应该补充一点,错误突出显示的行是:
while(!parser.EndOfData)
编辑:
我找到了问题与精确相同的问题,但尚未得到答复。不幸的是,他们的解决方案并不满足我。
一旦没有更多数据可用,从parser.ReadFields ()变成nothing / null。所以一个解决方法是用while(true)替换while(!parser.EndOfData),然后每次迭代检查字段是否为null,如果是的话早点退出循环。
在Visual Basic中,它看起来像这样:
True True
尝试
currentRow = MyReader。 ReadFields()
如果currentRow是Nothing然后
退出而
结束如果
'现在做所有其他
结束尝试
结束时
我不是C#(或VB)的专家,即使如此,我也会冒险猜测C#相当于:
while(true)
{
//定义列
string [] fields = parser.ReadFields();
if(fields == null){
break;
}
// int eanNumber = int.Parse(fields [0]);
// int catalogueNumber = int.Parse(fields [1]);
// double quantity = double.Parse(fields [2]);
// DateTime availibilityDate = Convert.ToDateTime(fields [3]);
Console.WriteLine(fields [0] ++ fields [1]);
}
也许有人对它更熟悉一点可能足以解决任何问题语法错误。
由于@casperOne表示真正的答案将在TextFieldParser的源代码中 - 这对我来说看起来像一个错误。
I am reading a CSV file from an FTP server and parsing it on the fly.
However, I stumbled upon System.ObjectDisposedException in the middle of the file being read. I know for granted that the file is being read, since my console is already giving me output.
Most of the answers I found online are regarding people closing the stream/parser before the reading is completed, but I cannot see such a mistake in my code.
Here is the problematic method:
public static void readCSVfromFTP(string ftpFileAddress)
{
// Setup FTP Request
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFileAddress);
ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
// Setup Response Stream & encoding
Stream responseStream = response.GetResponseStream();
// Text parser
using (TextFieldParser parser = new TextFieldParser(responseStream))
{
// Text parser settings
parser.SetDelimiters(new string[] { "," });
parser.HasFieldsEnclosedInQuotes = false;
// Text tokenization logic & file parsing
while (!parser.EndOfData)
{
// Define columns
string[] fields = parser.ReadFields();
//int eanNumber = int.Parse(fields[0]);
//int catalogueNumber = int.Parse(fields[1]);
//double quantity = double.Parse(fields[2]);
//DateTime availibilityDate = Convert.ToDateTime(fields[3]);
Console.WriteLine(fields[0] + " " + fields[1]);
}
}
// Clean up and close streams
responseStream.Close();
response.Close();
}
I should add, that the line which error highlights is:
while (!parser.EndOfData)
EDIT:
I have found a question with the exact same problem, yet it is unanswered. Unfortunately, their solution doesn't satisfy me.
Once there is no more data available, the value returned from parser.ReadFields() becomes nothing / null. So a workaround is to replace the while (!parser.EndOfData) with a While (true) and then each iteration check if fields is null, exiting the loop early if it is.
In Visual Basic it looks like this:
While True
Try
currentRow = MyReader.ReadFields()
If currentRow Is Nothing Then
Exit While
End If
'Now do everything else
End Try
End While
I'm no expert on C# ( or VB for that matter), even so I'll hazard a guess that the C# equivalent would look something like:
while (true)
{
// Define columns
string[] fields = parser.ReadFields();
if (fields == null){
break;
}
//int eanNumber = int.Parse(fields[0]);
//int catalogueNumber = int.Parse(fields[1]);
//double quantity = double.Parse(fields[2]);
//DateTime availibilityDate = Convert.ToDateTime(fields[3]);
Console.WriteLine(fields[0] + " " + fields[1]);
}
Perhaps someone with a bit more familiarity with it might be kind enough to fix any syntax errors in the above.
As @casperOne says the real answer would be within the source of TextFieldParser - this looks like a bug to me.
这篇关于在解析和读取FTP文件之前处理C#流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!