The following post is the first in a series by Raul Pefaur on Learning C#. Over the last month Raul has taught himself C# with a variety of projects, tutorials and books which he will describe to help others on their journey to using C# for finance. Raul has a Master of Finance and lives in Santiago, Chile. Yahoo has a popular API which lets you download daily financial data from its enormous library. In this blog post, I will be using this API to download financial data through a C# console application. It was created in Visual Studio and is free for you to download an use, though I recommend you try to build it yourself. If you like, use mine as a reference (I know there’s a lot of improvements in my code you could make. If you do, please share!).
Download C# Source Code for this ArticleDownload
The code consists of two sections: first, we need to enter the stock names and date ranges we want to download, and then communicate with the Yahoo Finance’s API to download the requested files.
Step 1: Building the stock names we want to download 
The program requests the user to enter the stock-tickers and date range they would like to download, or it will use its default values. The stock-tickers must be separated by a comma and listed on the yahoo finance website - finance.yahoo.com. When the default parameters are used the program will download the entire S&P500 stock-tickers, from the date range of 1st January 2000 to 1st January 2014. The list of S&P500 stock-tickers is pulled from a CSV file in the program folder and was originally sourced from Wikipedia. If you have a different set of symbols you would like to download you should modify this file with your own stock-tickers. Pseudo code:
Console.WriteLine("Enter stock names. If blank, all SP500 Stocks will be downloaded");
string userStocks = Console.ReadLine();
if (userStocks.Length == 0) { 
   // Download all S&P500 stocks
}
else { 
   // Read user input and download those stocks
}
Step 2: Downloading from Yahoo Finance API
For downloading the CSV files with historical prices from Yahoo Finance’s API, we need the following URL structure:

Every url starts as:

https://ichart.yahoo.com/table.csv?s=

Then, it needs a stock name (e.g.: Microsoft):

https://ichart.yahoo.com/table.csv?s=MSFT

A From Date (e.g.: 01/01/2000):

https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000

A To Date (e.g.: 24/12/2014):

https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=11&e=24&f=2014

Resolution of the data (daily, weekly, etc):

https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=11&e=24&f=2014&g=w

File format:

https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=11&e=24&f=2014&g=w&ignore=.csv

We construct this url-address from variables inside the program, so that it changes for each value of the stock-ticker and the dates required (that were built in Step 1). Months are written indexed from zero: so 0 is January, 11 is December. We will save all the files to separate directories. The first time the program runs these directories do not exist, so we must create them. The following code checks if the directory exists, and creates it if it doesn't:
// Create the directories in case they don't exist
if (!Directory.Exists(directory)) {
    Directory.CreateDirectory(directory);
    Console.WriteLine("Directory created.");
}
We don’t want to waste time in downloading files that we may already have downloaded. Therefore, we build a condition that checks if the file already exists in the directory. We use the .NET WebClient class to process the download request and save it directly to a file.
// If the file has not been downloaded yet:
if (!File.Exists(dayFile)) {
    webClient.DownloadFile(url, dayFile);
    Console.WriteLine(stock + " Data downloaded successfully!");
} else {
    //If the file already exists in the directory
    Console.WriteLine( stock + " file already exists");
}
Its important to wrap the WebClient in a "Try-Catch" loop to handle 404 or web time out exceptions. This code downloads the files separated in folders according to its data resolution (daily or weekly bars), for each stock. It takes a while if you are downloading all the S&P500 stocks, but it is great fun! Hope you like the app, I really encourage beginners in C# to try code it yourself, it took me a while but the results are great. Feel free to post your questions, and goodluck learning C#! Feel free to download the source code here: C# Yahoo Finance Update: Thanks to Jonathan Evans for his suggested updates to the code, they are now included in the download file.