File Downloading using WebClient's DownloadFile me
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   Input / Output
Current Lesson:
File Downloading using WebClient's DownloadFile me
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  ASP.NET 2.0 – What’s New? [next Lesson]  Master Pages in ASP.NET 2.0
File Downloading using WebClient's DownloadFile me
  by: webmaster

Have you ever had the need to download a bunch of files and didn't want to type them in manually into your browser?

Have you ever been to a webpage that lists files in non-hyperlinked lists?

Do you want to learn how to download files using ASP.NET?

If you answered yes to any of the above questions, then you have justified my time coding this ASP.NET file downloading console application.

So let's get started! Let's get an overview of the .NET framework namespaces I'll be using.

System.Net Namespace

So the system.net namespace provides you with a API to many of the networking related protocols used on networks etc. You will find classes Dns, IPAddress, HttpWebRequest, WebProxy and the one we will be using today WebClient.

So the WebClient Class enables you to send and receive data from a URI (Universal Resource Identifiers - see definition). The class contains many properties such as BaseAddress, Headers, Querystring and ResponseHeaders. As for the public methods, you'll find DownloadData, Downloadfile (that I use for this application), OpenRead / OpenWrite, UploadData, UploadFile etc.

So as I noted earlier, I will be using the DownloadFile method, note that the DownloadData method is very similar except that it downloads the data into a byte array.

How the Console Application Downloads Files

My console application (which you can download the c# source code for) is very simple. Let's take a look at the main method that performs the file reading and the actual downloading and saving of the files to a directory.

// get files to download
//
ArrayList rgFiles = Downloader.LoadFiles(Globals.PathToData, "files.txt");
// download
//
Downloader.DownloadAndSave(rgFiles, "../../Data/");
Console.WriteLine("..end " + DateTime.Now);
Console.ReadLine();

We first grab all the files we want to download and load them into an ArrayList.

ArrayList rgFiles = Downloader.LoadFiles(Globals.PathToData, "files.txt");

The file is loaded by using a StreamReader. I simply run through the file line by line (using the Peak method) and store the URI in the file to my ArrayList. I ensure the file is not empty.

StreamReader sr = new StreamReader(strPath);
ArrayList rgFiles = new ArrayList(); 
while(sr.Peek() >= 0) 
{ 
 string line = sr.ReadLine(); 
 
 if(line.Length > 0)
 {    
  rgFiles.Add(line);
 }
} 

1 2  (Page 2) -->


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  Input / Output
Current Lesson:
File Downloading using WebClient's DownloadFile me
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  ASP.NET 2.0 – What’s New? [next Lesson]  Master Pages in ASP.NET 2.0


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
snaso 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center