This course will be retired on July 14, 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
Well done!
You have completed C# Streams and Data Processing!
You have completed C# Streams and Data Processing!
Preview
Learn how to use classes in the System.IO namespace to find and retrieve directory and file information.
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
We'll be using Visual Studio
2015 Community Edition to
0:00
build a console application.
0:04
Be sure to follow along and
pause the video, or
0:06
slow me down if you need to catch up.
0:08
Now, let's create a project.
0:11
So we'll go to File>New>Project.
0:13
And in the templates here,
I'll choose Console Application.
0:15
Since we wanna analyze soccer
statistics in this application,
0:22
we can name it Soccer Stats.
0:25
I'm gonna show you some useful tools
that we'll need when working with
0:30
files on the file system.
0:33
We're going to need to add
the System.IO namespace,
0:35
because we'll be working
with IO operations.
0:39
Using system.io his namespace contains
a lot of the classes will be working with.
0:41
First let's create a text file
in our project, right-click and
0:51
add item, then I'll choose
general category over here, and
0:57
Text File and we can name it data.text.
1:03
Eventually we're going to use
this file in our application but
1:09
will need to change
a property on it though so
1:12
that it will be copied to the output
directory when we compile our program.
1:14
Right-click and choose properties.
1:19
It's down here.
1:22
Copy to Output Directory, and
I'll change that to Copy if newer.
1:23
This will copy our text file to the output
directory when we build or deploy but
1:29
only if it's changed.
1:34
Let's build, and we'll see if the file
is put into the output directory.
1:36
I'm going to copy this file path here.
1:42
And we can go check out our text file.
1:45
CTRL+C to copy.
1:47
And I've got an Explorer
window right here.
1:51
Paste it into the address bar and
there's our file.
1:54
Let's explore some of the stuff
we can do with the file system.
2:00
The directory class, directory is
2:04
a static class in the system.IO namespace
that gives us some static methods for
2:08
traversing directories or
folders on your system.
2:12
It's got a bunch of static methods for
us to create files or
2:16
list files in a directory.
2:19
Let's see what we've got
here in IntelliSense.
2:21
All right, we've got CreateDirectory,
see if it exists.
2:24
EnumerateFiles that'll list files.
2:30
Hm, this one looks useful,
GetCurrentDirectory.
2:33
It gets the current working
directory of the application.
2:37
Let's call that and
we'll assign it to a string.
2:40
Called currentDirectory,
2:43
And we need to call
the method in semi-colon.
2:50
We'll set a break point at the end
of our main method here so
2:54
we can see what the value
of the directory string is.
2:57
We'll run it with F5.
3:00
And I can hover over it to see the value.
3:05
So, there's a couple of
things we should note here.
3:09
There are two backslashes that
are separating each directory segment.
3:12
In a string, the backslash is a special
character that's used in escape sequences.
3:17
We'll get into the different string
escape sequences a little later on
3:22
in this course.
3:25
For now, though,
in this string each set of two backslashes
3:26
represents a single
backslash in the file path.
3:30
And since our app is
running in debug mode,
3:33
executable is compiled into the bin
debug folder under our solution folder.
3:36
Let's go ahead and stop debugging.
3:42
There's another class called
DirectoryInfo that we can
3:46
use when working with directories.
3:49
We can instantiate a DirectoryInfo object,
3:51
directory, and new DirectoryInfo, and
3:55
we'll pass it the string
of the current directory.
3:59
The DirectoryInfo object
now gives us methods for
4:07
working with the directory itself or
the files it contains.
4:09
Let's go check out the MSDN documentation
on the class by hitting F1 while
4:13
the cursor is on DirectoryInfo.
4:17
Well, we've got some
4:22
typical file system properties like
creation time and last right time.
4:25
Let's check out the methods.
4:29
We can use the get files method to return
a file list from the current directory.
4:35
Let's do that in our console application.
4:42
So I'll do a Var files equals
directory dot get files.
4:46
Then we can do a loop to iterate over
the files and print out the names for
4:56
each var file in files.
5:00
Open and close curly brace and
Console.WriteLine and
5:06
we'll print out the file name.
5:12
Let's run it with F5.
5:19
There's our text file we created,
data.txt.
5:23
We can also give this
method a search pattern,
5:27
In the GetFiles method we can
use an asterisk as a wildcard.
5:33
So, to get all the text files we can
pass a string asterisk dot text.
5:36
Let's see what it gives us now
we only get our text file.
5:46
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