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
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:
This isn't very elegant but if anyone else comes across the above error know that downloading the file is an alternative.