C#: Simple .NET Console App To Loop Through CSV, Download Images

I recently had the need to write a .NET console app which opens up a CSV file and does some stuff. This app loops through each row, downloads an image from the URL in column two and saves it with a unique file name provided in column one.

I want to remember this. So here it is.

using System;
using System.IO;
using System.Net;

namespace Download_Images
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				string root = @"C:";
					
				Console.WriteLine("Press any key to start.");
				Console.ReadLine();

				string[] records = File.ReadAllLines(root + "urls.csv");

				string destination = root + @"images";

				foreach (string record in records)
				{
					string[] fields = record.Split(',');
					string guid = fields[0];
					string imageUrl = fields[1];
					WebClient fileReader = new WebClient();
					string newFile = destination + guid + ".jpg";

					Console.WriteLine("Starting download: " + imageUrl);

					try
					{
						if (!File.Exists(newFile))
						{
							fileReader.DownloadFile(imageUrl.Trim(), newFile);
							Console.WriteLine("File saved: " + newFile);
						}
						else
						{
							Console.WriteLine("File exists. Skipping. " + guid + ".jpg");
						}

					}
					catch (Exception ex) {
						Console.WriteLine("Failed: " + ex.Message);
						if (ex.InnerException != null)
						{
							Console.WriteLine("Inner Exception: " + ex.InnerException.Message);
						}
					}

					Console.WriteLine();
				}

				Console.WriteLine("All done. Press any key to exit.");
				Console.ReadLine();
			}
			catch (Exception ex) {
				Console.WriteLine("There was an exception:");
				Console.WriteLine(ex.Message);

			}
		}
	}
}

Posted in: Code Samples, Development  |  Tagged with: , ,  |  Leave a comment

Leave a Reply

Most recent work

  • Our Restroom Our Restroom is an (inter)national crowdsourced campaign who’s goal is simple: compel businesses to make their single occupancy restrooms gender neutral. Championed by Kristin Russo, designed by Allison Weiss, built by me on WordPress.
  • How To Win At Feminism The badasses over at Reductress are at it again with their new book How To Win At Feminism.
  • Reductress I crank out on-going web projects for Reductress. Super fun as you can imagine.
  • Everyone Is Gay Everyone Is Gay is an indispensable question and answer resource for the LGBTQ community. Hell, it’s great advice for anyone with a body with an emotion or two. I made the site responsive among other things. Give it a look see.

Need hosting?

  • Media Temple My go to hosting provider. You get rockstar hosting and I make a little cash. No pressure, just clean honest fun.

Past work

  • Christine Chaney Creative The online presence of Seattle artist Christine Chaney. Specializing in architecture, art and apparel.
  • Get Your Shit Together This is a site dedicated to helping you get your shit together before an unexpected tragedy, like the loss of a loved one.
  • IHG Instagram Sweepstakes An awesome Instagram driven sweepstakes site for IHG.
  • Trivial Beersuit A site that provides professional pub trivia to bars and restaurants.
  • Audi YouTube Channel This site was a quick turn-around hair pulling adventure for the Super Bowl: revamping Audi’s YouTube Channel.
  • Teach.org The new Teach.org. Providing tools to help students become teachers.
  • Valhalla DSP A one man Seattle based company, writing pretty sick professional audio plugins. Check ’em out!
  • Microsoft Visual Studio 2012 Launch This sweet new Visual Studio 2012 Launch website sports a mobile first, responsive web design architecture and comes in 14 different languages.
  • Nice Manners Music Nice Manners is an LA based recording studio offering their clients all sorts of digital audio services. This is a demo of an internal genre based music search & player I built for them. I am ecstatic about launching this project—check it out.
  • Holiday Inn Tumblr A quick, fully responsive update to the Holiday Inn Tumblr page.
  • Microsoft's Art of Touch An artful HTML5 Microsoft website. Sadly, this site is no longer live.
  • Microsoft Hardware Healthy Computing Tool A useful little tool to help you find the perfect Microsoft mouse or keyboard.
  • Pella Professional Pella manufactures high quality doors and windows. This is their professional site.
  • Imaging the World A non-profit dedicated to providing ultrasound services in remote areas of the world.
  • Ginny Ruffner: A not so still life A site for a documentary film about Seattle artist Ginny Ruffner.

Blogroll