I have faced a weird error.
I need to download some files from excel which are uploaded on today. Files on FTP have the timestamp appeded with the filename. The code is as below:
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(strUrl);
ftpRequest.Credentials = new NetworkCredential(strUserName, strPassword);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
Stream responseStream = response.GetResponseStream();
string line = streamReader.ReadLine();
while (!streamReader.EndOfStream)
{
if (line.Contains(DateTime.Now.ToString("yyyyMMdd")))
{
strDownloadFilesName.Add(line);
downloadPath = downloadPath + line;
byte[] buffer = new byte[2048];
FileStream fs = new FileStream(downloadPath, FileMode.Create);
int ReadCount = responseStream.Read(buffer, 0, buffer.Length);
while (ReadCount > 0)
{
fs.Write(buffer, 0, ReadCount);
ReadCount = responseStream.Read(buffer, 0, buffer.Length);
}
string ResponseDescription = response.StatusDescription;
//fs.Close();
//responseStream.Close();
}
line = streamReader.ReadLine();
}
in the above code, once the if condition is true then after some iterations,
line = streamReader.ReadLine();
this line throws the error of:
Cannot access dispose object.
Object name: 'System.Net.Sockets.NetworkStream'.
Please help me out with it. Thanks in advance.
As a part of solution of the problem, I need to make different calls to achieve it instead of looping it by calling single time.