This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Use StreamReader to read CSV file into an application.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I've been keeping track of soccer
game results in a Google spreadsheet.
0:00
We can use this data to do some analysis.
0:04
Here we've got a date and
time the match was held, team name,
0:07
whether it was a home or an away game.
0:11
Number of goals, goal attempts,
shots on goal, shots off goal, and
0:13
a possession percentage.
0:18
I wanna get this data into
our console application.
0:20
First we'll need to get this spreadsheet
into a file on our hard drive.
0:24
In Google Sheets we can download
a spreadsheet in various formats with
0:27
File > Download as.
0:31
And we'll choose Comma-separated values,
also known as a CSV file.
0:35
This is good for
us because it's a text file.
0:40
You can get this CSV file in
the project files links in
0:43
the download section below this video.
0:46
Now we'll need to add it to our solution.
0:49
Right-click on the project and
choose Add > Existing Item.
0:52
Then we'll need to go to
our Downloads folder, and
0:59
we'll need to change this to All Files.
1:03
There it is.
1:07
I wanna rename this,
take off this Sheet1 here.
1:08
Okay, let's open it up to
see what it looks like.
1:15
Here at the top we've got the names of
each of our columns in the spreadsheet,
1:19
and then the lines after
that we've got our data.
1:24
Each value from the spreadsheet
is separated by a comma.
1:26
We need to open this file and
1:30
read the data in order to
get it into our application.
1:31
Just like we did with the text file,
we can use a stream reader.
1:35
We also need to change the Copy to Output
Directory property to Copy if newer.
1:38
Back in our Program class let's
collapse the Main method, and
1:47
we'll create a new method.
1:51
We'll call it ReadFile,
and it'll return a string.
1:54
Public static string ReadFile.
1:57
And we'll give it a string parameter for
the file name.
2:03
FileName, open close curly brace.
2:06
Then inside this method we'll use
the stream reader to load up the file and
2:11
return its contents.
2:15
Using(var reader = new StreamReader),
2:17
and we'll pass it the fileName.
2:23
Then a using block.
2:29
Then we'll return reader.ReadToEnd.
2:31
That'll return everything
inside the text file.
2:38
We'll need to get the file name to pass
to our method like we did with our
2:42
data.text file.
2:45
Let's go back up to Main, and
we'll delete this stuff here.
2:47
We'll change data.txt to
SoccerGameResults.csv.
2:55
Then we'll create a variable,
var fileContents = ReadFile,
3:02
and we'll pass it our fileName variable,
3:09
And then we'll just write everything to
the console, WriteLine(fileContents).
3:17
Let's see what it gives us.
3:26
I'll run with Ctrl+F5.
3:27
There's our data.
3:30
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up