Auto-resuming HTTP download C#

In C#, is there a simple way to resume an download over http, transparantly?

For example, i call a function to download a file x to drive c:. In the middle of the operation, I close the downloading program. The program would abort the transfer, but not delete the partially downladed file x.

I start the program, and once again call the function to download the same file x to driver c:. The function would realize that there is already a part of file x present on drive c: and then resume the download.

Anything simple in .NET 2.0 library that would allow that to happen?
Who is Participating?
dreadyConnect With a Mentor Commented:
The way to go, is first to check if the file allready exists on you computer, and if so, determine the length. What you want to do, is something like

if (File.Exists(txtPath.Text))
    startPoint = new FileInfo(txtPath.Text).Length;
    startPoint = 0;

Then you download using the startPoint value. Your download function could look like the following.
I copied the relevant code from the following URL:

private void Download(object startPoint)



        // Put the object argument into an int variable

        int startPointInt = Convert.ToInt32(startPoint);

        // Create a request to the file we are downloading

        webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);

        // Set the starting point of the request



        // Set default authentication for retrieving the file

        webRequest.Credentials = CredentialCache.DefaultCredentials;

        // Retrieve the response from the server

        webResponse = (HttpWebResponse)webRequest.GetResponse();

        // Ask the server for the file size and store it

        Int64 fileSize = webResponse.ContentLength;


        // Open the URL for download

        strResponse = webResponse.GetResponseStream();


        // Create a new file stream where we will be saving the data (local drive)

        if (startPointInt == 0)


            strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);




            strLocal = new FileStream(txtPath.Text, FileMode.Append, FileAccess.Write, FileShare.None);



        // It will store the current number of bytes we retrieved from the server

        int bytesSize = 0;

        // A buffer for storing and writing the data retrieved from the server

        byte[] downBuffer = new byte[2048];


        // Loop through the buffer until the buffer is empty

        while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)


            // Write the data from the buffer to the local hard drive

            strLocal.Write(downBuffer, 0, bytesSize);

            // Invoke the method that updates the form's label and progress bar

            this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });


            if (goPause == true)








        // When the above code has ended, close the streams




Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.