Skip to content

Instantly share code, notes, and snippets.

@csdear
Last active August 29, 2015 14:07
Show Gist options
  • Save csdear/bd0a09bcdde7350fee65 to your computer and use it in GitHub Desktop.
Save csdear/bd0a09bcdde7350fee65 to your computer and use it in GitHub Desktop.
.CSV to List Collection using StreamReader. Simple console demo, outputs contents of list to the console. e.g., 1 on the subject of StreamReader to List
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CONSOLE
{
class MainLoader
{
static void Main(string[] args)
{
//new reader object, opens .csv file.
var reader = new StreamReader(File.OpenRead(@"C:\Users\stuartd\Documents\GitHub\CONSOLE\CONSOLE\sample.csv"));
//a new array List of strings
List<string> list = new List<string>();
//while condition to loop through each line.
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
list.Add(values[0]);
list.Add(values[1]);
list.Add(values[2]);
}
//output section
foreach (string url in list)
{
Console.WriteLine(url);
}
}
}
}
**********************************************************************************
// Or, just one column to a list...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CONSOLE
{
class MainLoader
{
static void Main(string[] args)
{
//new reader object, opens .csv file.
var reader = new StreamReader(File.OpenRead(@"C:\Users\stuartd\Documents\GitHub\CONSOLE\CONSOLE\sample.csv"));
List<string> emailList = new List<string>();
//while condition to loop through each line.
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
emailList.Add(values[2]);
}
foreach (string url in emailList)
{
Console.WriteLine(url);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment