How do I programmatically download a file from a specified URL?

Posted on 2008-10-03
Last Modified: 2013-11-12
Hi Experts

I am looking for a snippet of code that will allow me to programmatically download a zip or xml file from a specified URL on the web. The code will have to work from a WPF program, so it is windows based. The URL and file will be on a domain that I administrate, so I will have full read/write access to the file. I will also want to save the file to a folder on the local C-drive.
Question by:Fritz41
  • 2
LVL 55

Accepted Solution

Jaime Olivares earned 200 total points
Comment Utility
use the System.Net.WebClient class, like:

System.Net.WebClient Client = new System.Net.WebClient ();
Client.DownloadFile("", @"c:\somefolder\");

Expert Comment

Comment Utility
Using a stream is the easiest and safest way to read a file on a client machine.  Hope this code helps:

Using Microsoft.Win32;

Using System.IO;

// Put this section of code in your function

/************ BEGIN ******************************************/

OpenFileDialog openDialog = new OpenFileDialog();

// Setup your file filters here

openDialog.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*";

// Variable that stores your file data

string fileData;

if (openDialog.ShowDialog() == true)


	using (Stream fileStream = openDialog.OpenFile())


		int numBytesToRead = (int)fileStream.Length;

		byte[] bytes = new byte[numBytesToRead];

		int numBytesRead = 0;

		while (numBytesToRead > 0)


			int n = fileStream.Read(bytes, numBytesRead, numBytesToRead);

			if (n == 0)


			numBytesRead += n;

			numBytesToRead -= n;



                  // This variable has your file data.

		fileData = Encoding.ASCII.GetString(bytes);



/************ END ******************************************/

Open in new window


Expert Comment

Comment Utility code requires the user to actually select the file.  So this might not work for you...sorry.

Author Closing Comment

Comment Utility
Thanks, this seems like exactly what I was looking for.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now