I get a System.ObjectDisposedException when I'm trying to download an XML from an FTP.
The first time I launch my app it works. I download file1. Then, I upload file2 and when my app will be launched again, it will try to download file2 instead of file1. However, it gives me the exception. The difference between file1 and file2 is just an extra line in the XML, I write down code and XMLs:
public String DownloadFile(String urlWithFile, int totalSize) { String data = ""; String dataFinal = ""; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(urlWithFile); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = _nc; request.KeepAlive = true; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { try { while ((data = reader.ReadLine()) != null) { dataFinal += data + "\n"; } } catch (Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); } } } if (Ready != null) { Ready(null, null); } return dataFinal; } I'm reading the following XML:
File 2 - I add an extra line and upload this one to be downloaded now)
Why does it work with file1 and not with file2?
They are exactly the same, only one node is added.
The upload function, just in case...:
public void UploadFile(String fileName) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_URLFTP+fileName); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = _nc; // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader(fileName); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); } The file is created with this function:
public String WriteToFile(int nserie) { try { localFileName = (nserie+1) + "-bbdd.xml"; xml.Save(localFileName); } catch(Exception ex) { MessageBox.Show("Problema guardando los datos en local"+ex.ToString()); } return localFileName; } Thanks.
No comments:
Post a Comment