1 00:00:00,810 --> 00:00:04,460 We'll be using Visual Studio 2015 Community Edition to 2 00:00:04,460 --> 00:00:06,490 build a console application. 3 00:00:06,490 --> 00:00:08,840 Be sure to follow along and pause the video, or 4 00:00:08,840 --> 00:00:11,320 slow me down if you need to catch up. 5 00:00:11,320 --> 00:00:13,390 Now, let's create a project. 6 00:00:13,390 --> 00:00:15,890 So we'll go to File>New>Project. 7 00:00:15,890 --> 00:00:20,930 And in the templates here, I'll choose Console Application. 8 00:00:22,460 --> 00:00:25,600 Since we wanna analyze soccer statistics in this application, 9 00:00:25,600 --> 00:00:27,730 we can name it Soccer Stats. 10 00:00:30,590 --> 00:00:33,370 I'm gonna show you some useful tools that we'll need when working with 11 00:00:33,370 --> 00:00:35,450 files on the file system. 12 00:00:35,450 --> 00:00:39,380 We're going to need to add the System.IO namespace, 13 00:00:39,380 --> 00:00:41,524 because we'll be working with IO operations. 14 00:00:41,524 --> 00:00:51,020 Using system.io his namespace contains a lot of the classes will be working with. 15 00:00:51,020 --> 00:00:57,691 First let's create a text file in our project, right-click and 16 00:00:57,691 --> 00:01:02,590 add item, then I'll choose general category over here, and 17 00:01:03,930 --> 00:01:09,430 Text File and we can name it data.text. 18 00:01:09,430 --> 00:01:12,300 Eventually we're going to use this file in our application but 19 00:01:12,300 --> 00:01:14,760 will need to change a property on it though so 20 00:01:14,760 --> 00:01:18,210 that it will be copied to the output directory when we compile our program. 21 00:01:19,210 --> 00:01:21,340 Right-click and choose properties. 22 00:01:22,390 --> 00:01:23,880 It's down here. 23 00:01:23,880 --> 00:01:28,750 Copy to Output Directory, and I'll change that to Copy if newer. 24 00:01:29,900 --> 00:01:34,360 This will copy our text file to the output directory when we build or deploy but 25 00:01:34,360 --> 00:01:36,470 only if it's changed. 26 00:01:36,470 --> 00:01:40,073 Let's build, and we'll see if the file is put into the output directory. 27 00:01:42,517 --> 00:01:44,740 I'm going to copy this file path here. 28 00:01:45,740 --> 00:01:47,988 And we can go check out our text file. 29 00:01:47,988 --> 00:01:51,570 CTRL+C to copy. 30 00:01:51,570 --> 00:01:54,520 And I've got an Explorer window right here. 31 00:01:54,520 --> 00:01:59,080 Paste it into the address bar and there's our file. 32 00:02:00,190 --> 00:02:02,640 Let's explore some of the stuff we can do with the file system. 33 00:02:04,060 --> 00:02:08,130 The directory class, directory is 34 00:02:08,130 --> 00:02:12,720 a static class in the system.IO namespace that gives us some static methods for 35 00:02:12,720 --> 00:02:16,730 traversing directories or folders on your system. 36 00:02:16,730 --> 00:02:19,820 It's got a bunch of static methods for us to create files or 37 00:02:19,820 --> 00:02:21,660 list files in a directory. 38 00:02:21,660 --> 00:02:23,329 Let's see what we've got here in IntelliSense. 39 00:02:24,920 --> 00:02:30,270 All right, we've got CreateDirectory, see if it exists. 40 00:02:30,270 --> 00:02:32,410 EnumerateFiles that'll list files. 41 00:02:33,470 --> 00:02:37,210 Hm, this one looks useful, GetCurrentDirectory. 42 00:02:37,210 --> 00:02:40,820 It gets the current working directory of the application. 43 00:02:40,820 --> 00:02:43,818 Let's call that and we'll assign it to a string. 44 00:02:43,818 --> 00:02:48,191 Called currentDirectory, 45 00:02:50,679 --> 00:02:54,350 And we need to call the method in semi-colon. 46 00:02:54,350 --> 00:02:57,490 We'll set a break point at the end of our main method here so 47 00:02:57,490 --> 00:03:00,570 we can see what the value of the directory string is. 48 00:03:00,570 --> 00:03:02,834 We'll run it with F5. 49 00:03:05,738 --> 00:03:08,340 And I can hover over it to see the value. 50 00:03:09,670 --> 00:03:12,810 So, there's a couple of things we should note here. 51 00:03:12,810 --> 00:03:16,550 There are two backslashes that are separating each directory segment. 52 00:03:17,710 --> 00:03:22,330 In a string, the backslash is a special character that's used in escape sequences. 53 00:03:22,330 --> 00:03:25,290 We'll get into the different string escape sequences a little later on 54 00:03:25,290 --> 00:03:26,400 in this course. 55 00:03:26,400 --> 00:03:30,490 For now, though, in this string each set of two backslashes 56 00:03:30,490 --> 00:03:32,610 represents a single backslash in the file path. 57 00:03:33,820 --> 00:03:36,840 And since our app is running in debug mode, 58 00:03:36,840 --> 00:03:42,870 executable is compiled into the bin debug folder under our solution folder. 59 00:03:42,870 --> 00:03:44,690 Let's go ahead and stop debugging. 60 00:03:46,310 --> 00:03:49,140 There's another class called DirectoryInfo that we can 61 00:03:49,140 --> 00:03:51,210 use when working with directories. 62 00:03:51,210 --> 00:03:55,533 We can instantiate a DirectoryInfo object, 63 00:03:55,533 --> 00:03:59,514 directory, and new DirectoryInfo, and 64 00:03:59,514 --> 00:04:04,532 we'll pass it the string of the current directory. 65 00:04:07,330 --> 00:04:09,851 The DirectoryInfo object now gives us methods for 66 00:04:09,851 --> 00:04:13,340 working with the directory itself or the files it contains. 67 00:04:13,340 --> 00:04:17,520 Let's go check out the MSDN documentation on the class by hitting F1 while 68 00:04:17,520 --> 00:04:22,315 the cursor is on DirectoryInfo. 69 00:04:22,315 --> 00:04:25,070 Well, we've got some 70 00:04:25,070 --> 00:04:29,800 typical file system properties like creation time and last right time. 71 00:04:29,800 --> 00:04:31,780 Let's check out the methods. 72 00:04:35,560 --> 00:04:41,690 We can use the get files method to return a file list from the current directory. 73 00:04:42,950 --> 00:04:44,580 Let's do that in our console application. 74 00:04:46,290 --> 00:04:54,840 So I'll do a Var files equals directory dot get files. 75 00:04:56,560 --> 00:05:00,770 Then we can do a loop to iterate over the files and print out the names for 76 00:05:00,770 --> 00:05:05,482 each var file in files. 77 00:05:06,950 --> 00:05:10,750 Open and close curly brace and Console.WriteLine and 78 00:05:12,950 --> 00:05:15,440 we'll print out the file name. 79 00:05:19,700 --> 00:05:21,110 Let's run it with F5. 80 00:05:23,220 --> 00:05:27,280 There's our text file we created, data.txt. 81 00:05:27,280 --> 00:05:29,714 We can also give this method a search pattern, 82 00:05:33,003 --> 00:05:36,950 In the GetFiles method we can use an asterisk as a wildcard. 83 00:05:36,950 --> 00:05:43,567 So, to get all the text files we can pass a string asterisk dot text. 84 00:05:46,563 --> 00:05:51,996 Let's see what it gives us now we only get our text file.