Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Win32
Tip/Trick

C# Web File Download with BITSadmin

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
27 Mar 2013CPOL 16.6K   7   1
When WebClient, HttpRequest, and all else fail, BITSadmin solves your download woes

Introduction

I was making an app that required downloading files from a website.  In the past, I’ve used WebClient (excellent for most scenarios), HttpWebRequest (clunky), and even the Forms WebBrowser (very messy) to accomplish the deed.  For this particular app however, all of these methods failed due to the dreaded “server protocol violation” error.  Argh!!!  With some digging, I found a console executable that was right for the job, so I made a simple method to use it in my app.  Success!  

Using the code 

A brief description of how to use the article or code. The
class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted"
like this:

C#
string local = @"C:\Users\metastruct\Desktop\my_image.jpg";

string remote = @"http://www.test.com/some_image.jpg”;

download(remote, local);

That easy to use!

The Code

C#
private void download(string remote_file_url, string local_file)
{
    try
    {
        /* Create the Process */
        System.Diagnostics.Process bitsadmin_process = new System.Diagnostics.Process();
        /* Assign the BITSadmin Executable and Arguements to the Process */
        bitsadmin_process.StartInfo = new System.Diagnostics.ProcessStartInfo("bitsadmin", "/transfer mydownloadjob  /download /priority normal " + remote_file_url + " " + local_file);
        /* Start the Process */
        bitsadmin_process.Start();
        /* Wait While the File Downloads */
        bitsadmin_process.WaitForExit();
        /* Dispose the Process */
        bitsadmin_process = null;
    }
    catch { }

    return;
} 

Such a simple solution to such an annoying problem.  Caution: downloading to a share drive gave me issues, so I downloaded locally and used File.Copy() to transfer.  Next step would be some improved error handling/redirection from the Process, but this works for now. Check out more at http://meta-struct.com/ 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralToo bad ;) Pin
Member 979571828-Mar-13 8:48
Member 979571828-Mar-13 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.