Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Tip/Trick

Download any file from website through console application in C#

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
7 Nov 2013CPOL 37.3K   12   9
Download any file from website through console application in C#.

Hi friends, in today's example Ii would show you how to download any file from any website using C# through a console application. For this we consider that we want to download some Excel file from http://www.testblog.com. As we see in the below snap, we have four links and we want to download those files through our console application.

 Image 1

For this first you have to download HtmlAgilityPack

C#
private static void DownloadXlsFiles(string filePath)  
{  
   WebClient wc = new WebClient();  
   var sourceCode = wc.DownloadString("www.testblog.com");  
   HtmlDocument doc = new HtmlDocument();  
   doc.LoadHtml(sourceCode);  
   var node = doc.DocumentNode;  
   var nodes = node.SelectNodes("//a");  
   List<string> links = new List<string>();  
   foreach (var item in nodes)  
   {  
     var link = item.Attributes["href"].Value;  
     links.Add(link.Contains("http") ? link : "www.testblog.com" + link);  
   }  
   List<string> xlsLinks = new List<string>();  
   foreach (string s in links)  
   {  
     if (s.LastIndexOf(".xls") != -1)  
     {  
       xlsLinks.Add(s.ToString());  
     }  
   }  
   foreach (string file in xlsLinks)  
   {  
     string[] fileName = file.Split('/');  
     if (fileName.Length > 0)  
     {  
       WebClient webClient = new WebClient();  
       webClient.DownloadFile(file, filePath + fileName[fileName.Length - 1].ToString());  
       Console.WriteLine(fileName[fileName.Length - 1].ToString() + " download successfully");  
     }  
   }  
   Console.WriteLine("All files download successfully");  
}

License

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


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

Comments and Discussions

 
QuestionHTMLAgilityPack Pin
bunty swapnil1-Jul-14 0:32
bunty swapnil1-Jul-14 0:32 
AnswerRe: HTMLAgilityPack Pin
Arjun Singh Faguda11-Jan-15 20:57
Arjun Singh Faguda11-Jan-15 20:57 
SuggestionResource Management Pin
Guillaume Leparmentier8-Nov-13 3:02
Guillaume Leparmentier8-Nov-13 3:02 
Hi Smile | :)

Just two small suggestions to have a well managed code execution :
- you should dispose your WebClient instances
- you may use the same WebClient instance between your 2 steps (page analysis and file(s) download)


+ could you explain a little how you would have done to download files from a website in which you need to be authenticated?

Thanks,
G.
GeneralRe: Resource Management Pin
Arjun Singh Faguda8-Nov-13 3:03
Arjun Singh Faguda8-Nov-13 3:03 
Questionwhy you say any? Pin
johannesnestler8-Nov-13 1:08
johannesnestler8-Nov-13 1:08 
AnswerRe: why you say any? Pin
Arjun Singh Faguda8-Nov-13 1:28
Arjun Singh Faguda8-Nov-13 1:28 
GeneralRe: why you say any? Pin
Murdine8-Nov-13 2:08
Murdine8-Nov-13 2:08 
GeneralRe: why you say any? Pin
Arjun Singh Faguda8-Nov-13 2:17
Arjun Singh Faguda8-Nov-13 2:17 
GeneralRe: why you say any? Pin
Murdine12-Nov-13 20:09
Murdine12-Nov-13 20:09 

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.