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