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

  • SEARCH
  • Home
  • 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 8417309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:00:18+00:00 2026-06-10T02:00:18+00:00

A while back I asked a question revolving around how to copy a file

  • 0

A while back I asked a question revolving around how to copy a file in chunks from one location to another: CopyFileEx "The parameter is invalid" error

I received the following code which was quite helpful.

    static void chunkCopyFile(string source, string destination, int bytesPerChunk)
    {
        uint bytesRead = 0;

        using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) {
            using (BinaryReader br = new BinaryReader(fs)) {
                using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
                    BinaryWriter bw = new BinaryWriter(fsDest);
                    byte[] buffer;

                    for (int i = 0; i < fs.Length; i += bytesPerChunk) {
                        buffer = br.ReadBytes(bytesPerChunk);
                        bw.Write(buffer);
                        bytesRead += Convert.ToUInt32(bytesPerChunk);
                        updateProgress(bytesRead);
                    }
                }
            }
        }
    }

However, I now need to convert this code to use FTP instead. I tried the obvious of just passing the FTP path to the filestream but it gave me an error saying “unsupported”.

I already managed to get the file length, I’m just not sure how I can split the download into chunks. Any help is appreciated as always!

Code so far(not much)

static void chunkCopyFTPFile(string destination, int bytesPerChunk)
    {
        uint bytesRead = 0;

        fWR = (FtpWebRequest)WebRequest.Create("ftp://" + FTP_SERVER_NAME + "/test.txt");
        fWR.Method = WebRequestMethods.Ftp.DownloadFile;
        fWR.UseBinary = true;

        fWR.Credentials = new NetworkCredential(FTP_SERVER_USERNAME, FTP_SERVER_PASSWORD);

        FtpWebResponse response = (FtpWebResponse)fWR.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader sR = new StreamReader(responseStream);

        sR.ReadToEnd();

        sR.Close();
        response.Close();
    }

Final code (working):

using (Stream responseStream = response.GetResponseStream()) {
            using (BinaryReader bR = new BinaryReader(responseStream)) {
                using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
                    BinaryWriter bw = new BinaryWriter(fsDest);
                    int readCount;
                    byte[] buffer = new byte[bytesPerChunk];

                    readCount = responseStream.Read(buffer, 0, bytesPerChunk);
                    bytesRead += Convert.ToUInt32(readCount);
                    updateProgress(bytesRead);

                    while (readCount > 0) {
                        bw.Write(buffer, 0, readCount);
                        readCount = responseStream.Read(buffer, 0, bytesPerChunk);
                        bytesRead += Convert.ToUInt32(readCount);
                        updateProgress(bytesRead);
                    }
                }
            }
        }
  • 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-06-10T02:00:20+00:00Added an answer on June 10, 2026 at 2:00 am

    Ok here is what you could try. This code is not tested. So if it contains errors and you get it work please edit answer or point me to mistakes.

    static void chunkCopyFile(string source, string destination, int bytesPerChunk)
    {
        uint bytesRead = 0;
        //Instead of this:
        //using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) {
    
        //...some necessary stuff...
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
        //Use this:
        using(Stream fs = response.GetResponseStream()){
    
            using (BinaryReader br = new BinaryReader(fs)) {
                using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
                    BinaryWriter bw = new BinaryWriter(fsDest);
                    long cl = response.ContentLength;
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[bufferSize];
    
                    readCount = fs.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                       bw.Write(buffer, 0, readCount);
                       readCount = fs.Read(buffer, 0, bufferSize);
                       updateProgress(readCount);
                    }
                }
            }
        }
    }
    

    References:

    http://msdn.microsoft.com/en-us/library/ms229711

    http://www.codeproject.com/Articles/17202/Simple-FTP-demo-application-using-C-Net-2-0 (Download method code)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I asked a question a while back about securing downloads using C# (
I asked a question a while back on here regarding caching data for a
A while back, I asked a question on here about how to run an
I asked a similar question here a while back but all the answers were
Ok so while back I asked question Beginner ASP.net question handling url link I
I asked this question a while back and even though I put up several
I noticed that a while back a question was asked about personal preferences in
This is related to this question I asked a while back. The end game
This is continuation to the question I already asked a while back. I've been
I asked this question a while back but now I'm looking to implement an

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.