问题
I am writing a method that connects to an FTP, reads a csv file into a stream then uses a TextFieldParser to process the data.
It's all working except for an issue I'm getting when it gets about halfway through reading the CSV suddenly I get an ObjectDisposedException exception. I've tried passing both the StreamReader & TextReader to the TextFieldParser but both result in the same problem.
Should I be downloading the CSV to a temporary local directory and then reading that or is there no issue reading a file from an FTP? I figured there might be some server setting possibly timing out the stream before it reads the entire file.
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("REMOVED.csv");
request.Credentials = new NetworkCredential("xyz", "*******");
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (TextReader reader = new StreamReader(stream))
{
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.HasFieldsEnclosedInQuotes = true;
parser.Delimiters = new string[] { "," };
while (!parser.EndOfData) //exception is thrown here about 1500lines into csv
{
Console.WriteLine(parser.ReadLine().ToString());
}
}
}
}
}
LAST LINE OF OUTPUT BEFORE EXCEPTION THROWN
190500,Courier Delivery,Freight,Distributor,1,5,15/12/2014 16:44
If I should be downloading the file first, do I just use WebClient.DownloadFile() ?? How can I tell when the file has finished downloading to then read it?
EDIT:
System net tracing output shows the following
System.Net.Sockets Verbose: 0 : [10412] Exiting Socket#24914721::Receive() -> Int32#95 System.Net Information: 0 : [10412] FtpControlStream#15315213 - Received response [226-File successfully transferred 226 5.748 seconds (measured here), 30.91 Kbytes per second] System.Net Information: 0 : [10412] FtpWebRequest#16868352::(Releasing FTP connection#15315213.)
FURTHER EDIT
The output from System.Net Tracing shows the last line of the CSV being received, so why is the parser being Disposed before it finishes?? I'm still quite new to programming so I'm not really sure how to proceed
回答1:
So I was unable to figure out how to stop the stream disposing before I read to the end with my TextFieldParser, instead I did the following:
Connect to FTP Read the file into a stream Copy the stream to a FileStream and create a temporary file Read the temporary file Delete the temporary file
This isn't very elegant but if anyone else comes across the above error know that downloading the file is an alternative.
回答2:
Use the Peek method instead of EndOfData - havent tested this, but should work...
//...
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
while (reader.Peek() != -1) //use Peek instead
{
Console.WriteLine(reader.ReadLine());
}
}
}
}
//...
来源:https://stackoverflow.com/questions/27976280/stream-is-disposing-before-endofdata-is-true