Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:06:04+00:00 2026-05-15T22:06:04+00:00

Before downloading a file from ftp server, I want to check it if exist

  • 0

Before downloading a file from ftp server, I want to check it if exist or not, if not it mu
st throw exception. The code sample works when the file does not exist. However when the file exist, after execution the line; “ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;” it jumps the second catch block and prints “Error: This operation cannot be performed after the request has been submitted.” what’s the point that i can’t see..Thanks for 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 + " is downloaded from 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 + " does not exists on ftp server");
                     System.Diagnostics.Debug.WriteLine("Error: " + fileName + " is not available on fpt server");
                 }
            }   
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:06:05+00:00Added an answer on May 15, 2026 at 10:06 pm

    My understanding is that you have to create a new FtpWebRequest for each request you make. So before setting the Method again you’d have to create a new one and set the credentials again. So pretty much that you’d have to repeat the following two lines:

    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
    ftpRequest.Credentials = new NetworkCredential(userName, password);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Service that is downloading a file from a server. I warn
I plan to use ASIHttpRequest for downloading files from back-end server. Before actions, post
I'm downloading (from server to client) a file using a WebClient object: WebClient wc
I'm downloading an entire directory from a web server. It works OK, but I
I am downloading zip files from FTP server using PHP ftp function.I used binary
ok i am downloading a file from a server and i plan to delete
I'm using these code to download a file from the server. The link of
Before I begin I'll freely admit it's something in my code but it's not
My FTP client (apache commons ftpclient) needs to download file from FTP. The problem
I am trying to download a file from a server in a user specified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.