Solved

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

Posted on 2008-10-03
4
328 Views
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.
0
Comment
Question by:Fritz41
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 22639687
use the System.Net.WebClient class, like:

System.Net.WebClient Client = new System.Net.WebClient ();
Client.DownloadFile("http://www.yourdomain.com/yourfile.zip", @"c:\somefolder\yourfile.zip");
0
 

Expert Comment

by:esesjay4
ID: 22639696
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)

				break;
 

			numBytesRead += n;

			numBytesToRead -= n;

		}

		fileStream.Close();
 

                  // This variable has your file data.

		fileData = Encoding.ASCII.GetString(bytes);

	}

}
 

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

Open in new window

0
 

Expert Comment

by:esesjay4
ID: 22639702
whoops...my code requires the user to actually select the file.  So this might not work for you...sorry.
0
 

Author Closing Comment

by:Fritz41
ID: 31502982
Thanks, this seems like exactly what I was looking for.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 40
Change Format on Data when exporting to Excel 4 23
SQL Exceptions 3 40
Close form "before" open 3 30
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

21 Experts available now in Live!

Get 1:1 Help Now