Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Web File Download with BITSadmin

0.00/5 (No votes)
27 Mar 2013 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:

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

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here