c# - Checking if file exist or not on ftp server before downloading it -


before downloading file ftp server, want check if exist or not, if not mu st throw exception. code sample works when file not exist. when file exist, after execution line; "ftprequest.method = webrequestmethods.ftp.downloadfile;" jumps second catch block , prints "error: operation cannot performed after request has been submitted." what's point can't see..thanks answers.

public void filedownload(string filename)         {             stream = new filestream(filepath + filename, filemode.create);             ftprequest = (ftpwebrequest)ftpwebrequest.create(new uri(ftppath + filename));             ftprequest.credentials = new networkcredential(username, password);             ftprequest.method = webrequestmethods.ftp.getfilesize;              try             {                 response = (ftpwebresponse)ftprequest.getresponse();                 ftprequest.method = webrequestmethods.ftp.downloadfile;                 ftprequest.usebinary = true;                 response = (ftpwebresponse)ftprequest.getresponse();                 ftpstream = response.getresponsestream();                 cl = response.contentlength;                 buffersize = 2048;                 buffer = new byte[buffersize];                 readcount = ftpstream.read(buffer, 0, buffersize);                  while (readcount > 0)                 {                     stream.write(buffer, 0, readcount);                     readcount = ftpstream.read(buffer, 0, buffersize);                 }                  ftpstream.close();                 stream.close();                 response.close();                 console.writeline("file : " + filename + " downloaded ftp server");              }             catch (webexception ex)             {                  ftpwebresponse res = (ftpwebresponse)ex.response;                  if (res.statuscode == ftpstatuscode.actionnottakenfileunavailable)                  {                      stream.close();                      file.delete(filepath + filename);                      console.writeline("file : " + filename + " not exists on ftp server");                      system.diagnostics.debug.writeline("error: " + filename + " not available on fpt server");                  }             }                catch (exception ex)             {                 system.diagnostics.debug.writeline("error: " + ex.message);             }         } 

my understanding have create new ftpwebrequest each request make. before setting method again you'd have create new 1 , set credentials again. pretty you'd have repeat following 2 lines:

ftprequest = (ftpwebrequest)ftpwebrequest.create(new uri(ftppath + filename)); ftprequest.credentials = new networkcredential(username, password); 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -