C# Programming: Working with files in C# | Lukas Vyhnalek | Skillshare

C# Programming: Working with files in C#

Lukas Vyhnalek, Microsoft Employee, Programming Teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
6 Lessons (43m)
    • 1. Introduction

    • 2. Working with C# Files PART 1

    • 3. How to create file?

    • 4. How to read?

    • 5. How to write?

    • 6. How to get info about the file?


About This Class

Hi, in this class you will learn how to work with files in C#.

You will learn pretty much everything you need to know in order to use files in your C# application. You will learn how to work with files and directories. More precisely how to create a new file and directory. Once we have the file created. I show you how you can read and write. There is a couple of ways you can read and write. I go through them all.

At the end of the class, I even show you how to get information about the file. For example, you can see when was the last time somebody edited the file. You can check what extension the file has and so much more.


1. Introduction: I ever run. And today's lecture I just want to talk a bit about what do you will learn in the following couple of lectures? So, basically, you will learn everything there is to learn about C sharp files. So everything that you can imagine, for example, how to open any file how to create a new file, Um, how to write into a file, right? How to open and then read the actual content off that file. So a lot of important things that if you will definitely use if you will create some sort of applications later on. So, for example, imagine that you are building. I have no some sort of text editor, right? You want the user to be able to save the text they are working on, right? You want them to save the file, right? And in order to do that, it's actually family easier, right? You don't necessarily have to implement the functionality off. No talking to operating system and stuff like dead because somebody else already implemented it. Okay, so what you're basically doing issue Just use decode that somebody else wrote for you and you can easily create a new file for example, Onley by writing a one line in your application. Okay, so once again, it is really, really important to know how to work with vials and see sharp. If you are creating pretty much any application used somehow needs work with flow's. And also it is not dead hard to learn this. It's it's really fairly straightforward. It's really not that hard. Trust me, it's only a couple of lines and you will be able to do crazy things. So, yeah, that's pretty much it. I hope you will enjoy the next couple of lectures and let's get started. 2. Working with C# Files PART 1: all right, gas. So we will start off easy. We will just create a new file, right? That's not dead big of a deal. Okay, so let me just show you how to do that. So first of all, as I said previously, you need to import some sort of functionality that somebody else wrote for you. OK? And you will do that by typing using system, okay? And then dot i o is a input output. Okay? And this basically means that you are just using some sort of file. You can think of it this way. It's actually a bit more complicated, but you can think of it that you are just using some sort of file that somebody else wrote did have a lot of functions that you can actually call and use. Right? So remember when we talked about functions? You You basically, um you don't necessarily have to know how function works in order to use it. So this is the exact case. You are just using some sort of functions. You don't know how they are implemented inside, but they provide use such a great functionality. Cool. So with this being said, you can now create a new file. And when you are working with vials, you can use the class file. Okay, that's pretty straightforward, right? You would probably call this this glass file because it works with files, right? And this glass is actually right. Was actually imported when we typed this using system that I Oh, OK, so this file is actually somewhere hidden inside this thing. Okay, Cool. And then the thing that you can do is actually creating a new file, so simply type in here, create, and then you need to basically specify De Bath to the file. So do you know how bath works usually type something like, See something? Something, you know, some for others. And then, for example, the name of the file and txt. This is a path, right? But if I, for example, try this and try to run this thing, we will get an error, Okay? And basically, this thing says that the directory was not found right, because I didn't have the, um right. It actually tell you doesn't tell you. Um, Does it tell you I don't see it? Okay. Um, yeah, but it actually doesn't tell you. Where was the problem, whether it was with the sea or with with this part. Right. Um, you probably know that it was with this part, but you are not sure. But anyways, um, if you define a proper bath, so, um, you should be able to get it. So I think that this one should be a proper path. Let me, actually, uh, just copy this, okay? I should be I have a on the second display. I have opened up. Yeah. Cool. I have opened up a basically a file explorer, right? And it can You can basically simply copy the path to your file from that. Right. Um, cool. So, yeah, if you don't know how to do it, it's just simply if you if you open this up and go in here, click right click and then copy address. Okay. And Advil, basically, then you can just simply based it into your application. Cool. But right now, as you can see, there are some sort of errors, right? If I run the application, okay. If I I would just put it on the next line so that we can see it. Um, like this cool. And yes, if I run the application, we will get some sort of error, right? Ah, we don't want to run this, but what it says Well, it will. If you click on the error, it will basically tell you Hey, inside here, you have unrecognized escape sequence. So basically, if you type this character into a string, it is some. It is something that is called escape sequence. Ah, which means that, um if you want to, for example, at some sort of thing into a text, like a new line, you usually use this escape sequence and n Okay, this will create a new line. Okay. Ah, And you also may notice that right now this thing is not there is no red underlining. Right, But inside here it is Red underlined because it doesn't make sense. Basically, if you take this this slash and then some sort of symbol, it can create some sort of kind of system. Think if you if you want in the string, you can add step. You can add new line and stuff like that. Okay, so but if you don't have the proper character behind it, it will basically tell you that you have an unrecognized escape sequence. So in order to deal with this, you can simply put and at and t at the beginning off the string. OK? And this will basically tell the computer Hey, this is a bath to a file, and these are not escape sequences. These are just folders. Right? So this is just text. Okay, Cool. So right now, if I just close this thing, it should actually work. So if I run this thing, let's see. Oh, we will once again get an error dead we get and, um, excess tonight. Okay. Okay. Because we cannot right into this file exactly into this folder. Exactly. Right. We need to go to the binary and then the buck, and then we can create a new file in there. So for how that takes me, Okay, so like this. And right now, when we were on this, we should be able to actually get the new file, right, because we have to get permission to write in there. Okay. And if I go ahead and open up the folder, right. This is the This is where the program is. Okay, So C sharp files. Is this this program right. And, um, when we are using the control and a five, it basically what it does it it actually creates a new file. And this is the execute herbal file, right? The restored in the bin. And then de Bock. Okay. And this vile is simply an executed will file. You can just Greek on that, and it will run, right? And it will run the application that we just created inside here. Okay. And as you can see in this folder, it actually created defiled that txt. So our new file, okay. And as you can see, it is exactly the This path is exactly to, uh, this folder, right? And usually the program doesn't have the option to actually right in some, for example. System folder. Right? Because it's for safety reasons, right? You don't want an application that somebody created and that somebody you probably don't trust that somebody right to actually create a new file on your disk everywhere that he wants, right, So it kind of makes sense that you can get the permission denied, So basically, you cannot right here. Um, when you are trying to, right, Okay. And we will learn how to handle it later on. Okay, But another thing that I want to show you is that you don't necessarily have to specify the bath to D folder, right? You can just type, for example, my text file that txt OK, And it will actually create the new file in the father. Okay, So if I go ahead and Randy's application and once again run properly and if I go back here , as you can see, there is the file, right? So it actually creates automatically A Z can see in here. We have d the basically when we run the application, it runs, and it remembers that it is stored right. The application, the execute execute herbal file is stored in this particular folder. Okay. And basically, when we are creating something, it automatically adds this folder in front of this. Okay, so we are basically telling the computer Hey, create this file with this name Exactly where the application runs. Okay, So exactly in the folder, Verdi execute Herbal s okay. And yes, so desperate. Much it for this lecture. I hope you enjoyed it. 3. How to create file?: Alright, guys. So another cool thing that you can actually do is create a new directory. So, for example, you have some sort of application, and you need to store at no, like settings. And then you also need to store some other files and you want to separate those two, and you don't want to store them and only like, one folder and keep it messier. I do want to clean it up and split those things into folders. Right. So what you can do is actually create a new folder and then store some files into that folder. So once again, what are we doing? Well, you are working with directory, okay? And what you want to do while you want to create a new directory, okay. And then you can once again specified bath. So exactly the same way as we did it. The Indy previous lecture. Right where we just took some path and put it in, um, bad. You can also do it this way. So, basically, in the folder, when where the application runs, you want to create a new folder. They chewed Genco, for example settings, and I will just close this app like. Sorry, I would put a semicolon at the end, and I will run it and it runs successfully. And as you can see, when I pulled the the file Explorer inside here, you can see that these settings folder is created inside here. OK, we created it with this line. Okay? With this 15 this line on the 15 and what he can do now is you can actually store something in the directory. So, for example, um, when you have a right, you don't necessarily have to do this every time, because the directory will be well, will not be deleted, even if you close the application. Okay, so when you close the application, the directory will stay okay. And also the singles with with files, it kind of makes sense. Right. Um, bar, what you can actually do is also store something in that directory, and you can simply type in here settings and then slash my text file. And this will basically tell the computer dead. Hey, I want to store this in this folder and then in I wanted to be called this thing. Okay. And if I run this application, as you can see it runs successfully. And when I pulled up this thing and click on the settings, as you can see, there is the father we just created. Okay. S. Oh, yeah. This is basically how you can work with directories. And another thing that I just want to show you in this lecture is that you can basically check ah, whether a file exists. So, for example, I will just write it down into the council, so right line and I will just check whether this file that I just created exists so file that exists, right? This can a straightforward right You want to check? But a file exists. You just type filed that exist. And then the name of the file, right? It's simple is that there's really no science to it. So, for example, I just type settings. Ah, slash my text file. Sorry. Text file that txt. Okay, like this and I will just put this line away right and run this application and it should bring out true, right? It does. Because these this file actually exists, right? If I put something like this inside here and run the application, it returned false. Because this file doesn't exist. And basically, also, this folder doesn't exist, right? So this is really fast way that you can check whether D file actually exists. And this is very useful when you are creating some sort of application. Where you when you get input from user, Right. So, for example, you ask the user to specify a bath to some sort of file, you need to check whether that path is correct. Right? Because if you would open something like that, Right. Um, and the file wasn't there. You would get some sort of error. Right? So you need to check this, OK? And this is really fast way to check whether a file exist. Um, yes. So this is pretty much it. Another thing that I forget to mention in the previous lecture is Dad, when you actually create a file, Another thing that I forgot to mention in the previous lecture is that when you actually create a file, um, you are basically checking weather. This file exists, right? Whether this exist, and if it does not, you create a new file. Okay, But if it does, do you know what happens? Well, let's try it out. Okay. So I will just put this folder inside here quick on these settings and write something in this file. Okay, So I will just write something, save it and close it. As you can see right now, the size is well, kill, but okay, that's fine. Um, about what happens when I run this application. And it basically should create a new file that have the same name as defied that we just did. We just edited, right? So what actually happened? Well, it deleted the previous file. Okay, so it deleted the previous file and created a new one with the same name. So be careful when you are creating a new file. You can, for example, check if the file exists, right? If the file does not exist, right, So file, um exists. It's false. Okay, So this file, okay, I'll just could be based. It eso if this file exists or not exist, OK, You want to create it, so you want to create it, so this line should go here, okay? And there's basically takes care off this problem, and the file is already there, right? You wouldn't override it. So for example, if I have the file and I type really something in there. Okay. Doesn't Yeah, their list. So if I say that and close this thing and get this thing out of the way and run this application, um, as you can see it browned smoothly And also, as you can see, the file is still untouched right there. Still the content. OK, so it didn't override D d file that we created previously. Okay, so that's pretty much it. 4. How to read?: Okay. So in the previous couple of lectures, you learned some sort of the basics, right? How to open a file, how to create a new file? Well, how to create a new for help and have to creating new directory. Right? But right now, things will get a bit more interesting because we will actually read the countin off the file. So let's get into it. First of all, I will write something Maurin this file. So I will just create a couple of more lines in there. So And we will read all of those lines, okay? In our application. So I will just save it, close it and move this away. And right now, let's actually do to do the work and actually create this thing. So Okay, so we got if the file does not exist, OK, so important negation inside here. Okay, so if that if it does not exist, we will create a new file. If it doesn't exist, we will read it. Okay, So read it. And this thing will be create new file, okay? And like this and yes, so let's actually read it. And as I mentioned earlier, it is really fairly easy to read a fire. So once again you just type file, then dot And then you can just there a couple of options you can read by bites. You can read all text. You can read all lines. So let me just start with the wheat or lines, OK? And this one will be the path will be just settings. And in my text file. Okay, my text file like this. And, um yeah, that should be it. So this read or lines basically returns a array off strings where each and every element in the array iss one line. Okay, so we will have a string array. We can call it, for example lines, and this will be equal to whatever we read from that fire. So basically, we will get an array off lines. Okay, each and every element will be one line. And what we can do is just simply print it out in the council, for example, just to check whether it waas whether we read it successfully. Right? So for each and every line, so we can just use some sort of index, I and we will check you will go through the files until while the I is smaller than the length of the lines. Right? So basically the count off the lines inside here, cool. And we will just move on to each and every Latin to the next line by the end, right? So we will increment I And what we want to do is just ah, right line. So right line. And then we just want to print out the lines from 02 lines length minus one. Okay. And if I just saved this thing, I think that this should actually read everything from the file. So if I run this thing, as you can see, it actually printed out all the content off that file right on each and every line. You have deadlines. So, for example, let's make things interesting and let's actually do some cool self. So let's actually read the file into a strength. So inside here you can also do What you can Also do is just defining new strength called fire content, for example, or whatever. And you can just read all of those files as a text Wheat all text. Okay. And it basically returns a strength. Right? Basically opens a text file, reads all lines off the file and then closes the file. Okay, so that's basically what this does. And we can just use this thing and copy paste this bath to it. So wow. Wow. What is happening? Okay, like this so pasted inside here, and, um, we will actually open the open and read the file off this thing, right? This off this path, and then we can just write it down into the council. So council right line, and then we have the file content. So file content like this, And if I run this application, as you can see, it printed out pretty much the same. So this is very other file starts, right? So this is the first print where we used the array off lines. And this is where we just use one string, right? One big string. That whole did the entire five, right? Distinct. So there are a couple of options how you can actually read it? Let me just show you how the, uh, read all bites works. So if I tap in here filed that Ah, read or bite. Okay. As you can see, the returns bite, Ray. Okay, and I will just show you in a minute. Howdy bite or a looks like. So I will just ah, store this into a bite. Ray called Let's just call attempt and it will basically read all bites. And then we can once again, it's right through it and then print it out. So apple just it rate through the length of the temp. Okay. And I will just print out ah on each and ever index from zero to the length minus one, and let's see what we get. This is pretty interesting, right? What? It iss Well, these are just numbers, and these are just bites. Um, so it's like, if you know, binary. Um, you you know, that one bite can be a Basically this text is actually represented by zeros and ones. Right And bite is basically eight zeros or ones behind it. So this is a some sort of a number, right? De text is actually a number, and I know this probably doesn't make sense. Bar Yes. Sometimes you want to use it, but this is a bit more advanced stuff, and I won't get into it in this lectures, so I just want to point out that there is something like this and that you can read all bite if you know what, by Tous, And if you know how to work with them, it can come out handy at some point. Cool. So right now let's do some cool stuff with this application. And what I mean by that is we can, for example, what we can do is ah, we can, for example, replace um all ace with bees, for example, in that file. Okay, So we will just, um yes, so we can get rid of this thing, or I will just commented out so that you can actually get it in the file. That will be as a resource to this lecture. And what you can actually do is just, um, replaced the ace with beasts in the strength. Right. So we have the file content very about, and we just want to do replace you want to replace a with Be OK, Why not? And obviously we want to do something more. Uh, you want to do some real stuff with this, but this is just an example that just crossed my mind that you may want to do at some point in your application, right? You want to replace something in a file with something different, and you can do it fairly easily Right now, we would also need to save it, right? So safe. Save the file and we will get to it to the Indy next lecture. Right? This will be the actual replace, and this would be actually make sure open and treat. Right. So open your feet. Okay, so on these lines, we open printed out. Then we replace it, and then we will save it and we will get to it in the following lecture. 5. How to write?: Okay, guys. So in the previous lecture, we ended up at this point. So we basically replaced a with bees, and we Well, we read the content off the file, replace it, and now we would like to save it. So we would like to actually save this file content and see, um, whether it will weather and will actually work. So inside here, when we open and read new file, you can see file that read all text. So what would you say is the name of a method that basically writes all text? I pretty much told you, right, So it is just filed, that of right all text. This is the name right? Then you just defined the the path to that file. So in our case, it will be the settings, um, slash my text file. That's the XY. Okay. And then we also need to pass a strength that is the content. And we can just type in here defile content like this and on a semi coin at the end and run this application, and it actually run successfully. And when I pulled out this thing, um, it actually says there is a lot of face, Okay? And right now, you can pause the video and try to think about what is the problem. And, um, it has to do something with this line, okay? And that's all I will tell you on. So if you didn't pause the video, do it now because I'm about to tell you how to actually fix this thing. So, uh, inside here, it's replaces these ace and base, Okay, but it returns a new kind of string. OK, so, um, replaced, We can call it, um, where we have the ace replaced with bees. Okay, so in the string, we already we have replaced a with bees bar. The fire content doesn't change. Okay, so it doesn't store the re replaced characters inside the file content variable. Okay, so inside here, we also need to write replaced instead of the foul content. And right now it should actually work. So if I run this thing and I will actually keep this open so that you can see the previous file and I will open also this thing up and see, as you can see there, there are a lot of bees and you cannot find any a because they were all replaced, right? And so if I just close this thing, as you can see, there were a lot of ace in there. Cool. Um, yes. Who? That is basically one simple thing that we did right from this thing from this simple example. One thing that you can know this is that this right, All text basically tries to write something in this file. Okay? Yeah. Cool thing. Is that what happens right now? Okay, that's that's another thing that I want to mention what happens right now. So the my text file to currently does not exist in this folder. Okay, so there are basically two options we eat or get an error that this file doesn't exist, or it will just create a new file. So let's actually run the application and find out. Okay? It created the new file. Right. Um, so if the file that you specified in this path okay does not exist, it will just create a new file and store it in the new file, okay? And if the file exists, it replaces D content. So it takes whatever is in that file and basically replace well, it kind of deletes the file and then creates a new one. Kind of. Okay, you can think of it this way. It deletes the old file and create a new file and store whatever we are writing in there. So another thing that you may want to do sometimes is just a bent something right? So appends something to the end off the file. Okay, So you have, um, had no I had no what you can have. I can't think of a example right now, but, uh, yeah, maybe a text editor, right? A user bright something in a file. Then he opens it up, right, saves it, opens it up, and then he writes something at the end, right? And you want to store. That's something that he added to that file to the end of the file that he previously open . Okay. For example, this is a weird example. I know, but so basically, each time you want to add something to the end of the file you to upend. So how can you do the upend operation? Well, you just I filed that appends all text. Okay? And this will basically once again, you specified bath and then you specify the string that you want to store, and it will. Actual ists add to the end of this file, right? It will add the strength. Okay, So if we get rid of this line, um, and run this application, we should see the difference inside here. So if I opened up the text file to, as you can see, there is a lot of lines, Okay, there's differently more lines than in the previous file. And then this one is through the where we didn't append. Right? So this is the origin of file that we read from. Okay, So basically, a pent adds these lines or the string at the end off the file, okay? And yes. So another thing that you can actually do is use something similar to this. So, for example, inside here, we had the read or lines eso you can also, when you are writing instead of right all text, you can just use right or lines. Okay. So right. All, uh, lines and I will once again create a new fire for this. So this will be free, and you need to call it with an array off strength. So strength, Ray, that lets go adjust new lines like this. And it will be an array containing a couple of lines, for example. Wow, this IHS cool. And let's write it down. So new lines like this. So we right into this file all of these lines, right? So every element will be on its new line. Okay, so let's actually around this application and see how it works. Okay? It runs properly. And if I blow up this thing and check out the file as you can see you out, this is cool. It actually stored everything in there. Okay. Um, yeah. And you can once again also write bytes, right? So basically, right, all bites and you would use bite array. Um, and you can also do a pen. So, for example, with this file, you can do file a band, all lines, and let's just say that we want to upend it, too. I know this thing. Okay, So, to the second file, and we want to I don't want to create new array, so I will just use this thing. Okay, but basically the same concept applies in here, right? Only this time, the a pent adds it to the end of the file. So if I run this thing, it runs without errors. And if I blow this thing up and this was the second right, we just created as you can see it it's like this, right? So why? Wow, this is cool. So, yeah, Another, maybe a bit important thing to keep in mind is that, um when you are actually writing in a file, um, and when you are a pending, it doesn't add the new line in front of it. Right? So what do you can actually do? Is just, for example, inside here, use the escape corrector. Right. So this is basically creates a should create a new line. So if I run this and once again, open up the file So my text file to and see right currently there is the new line and then wow, this is cool. Okay, so, um yeah. So dentist basically end. That is how you can write into files and see shop 6. How to get info about the file?: Okay, guys. So you already know how to open file, how to create new file, how to read from the file and how to write from the file and also have to append. But in this lecture, I will show you how you can actually get some information about the foul, and this will be ridicule. So first of all, you need to create a some sort of file info object, and you can call it, for example, info. And it will be a new instance of a new object. So this is basically, um you are telling computer Hey, creating new on instance off this class, This is from some object oriented programming stuff. Okay, that I'm talking about. Don't worry. If you don't understand it yet, and you will basically pass. Oh, sorry. You're basically past Ah, the name of the file in there. Okay, so it will basically tell you Hey, tell me some info about defile. That's what you are telling to computer. And then you can basically write line. So council dot right line about the fire so you can just bring doubt. For example, the directory, right? The directory. So basically the bath, the file leads to. Okay, so allergies. Copy and paste it, uh, to find out some more. But as you can see, diversity directory, the file is in. OK, so the entire path a two d file that is called my text file. Okay. And this is really cool, right? So a lot of times in the application, you actually need this thing. You actually need to somehow, um, I'd know, for example, check 40 path or stuff like that. This is really useful stuff. And you can also check 40 extension. Right? So you want to know whether it is a txt or whether it is a no doc or whatever extension it can have, right? You need to check sometimes. And if you ask the extension, it will basically tell you Hey, this is that txt. Okay, if it would be something different, um, right. You would actually have a different extension. Okay? And if the file does not have extension, right, let's see what happens if I inside here, create a fire without extension, and then I will check the info about this thing. So let's run this thing as you can see, it doesn't bring anything. Okay? because there is no extension, right? It kind of makes sense. OK, so cool. It's Let's just make it once again a text file, because I cannot look at this. You usually don't see it, right? This is really just and educates that I just wanted to show you. OK, cool. So another thing Dad you can do is basically check. Ready? Ready file iss read only. Okay, so this is this read only, And this basically tells you that you cannot write in the file. Okay? So you can only read from this file, right? So if this is true, you can only read If it is false, you can also write. And in this case, it is false, right? It kind of makes sense because we we have ridden into the file in previous lectures. I would be pretty surprised if this would be Drew. Okay, So let's actually take a look at another stuff that we can do. And this is really cool. You can also ask for the last access time, right? So basically, time, uh, when defy was accessed, right? As you can see, this is the date, and this is the time. Okay, so this is pretty cool. This is pretty cool stuff. For example, you want to check when is the last time user updated to defile right or something like this ? This is really, really useful. You can do it fairly easily. And you can also check, for example, last rites time. Okay, so last access time basically means that user opened Defile, right. If user opens the file or create a defile, it is an excess. Or if it read something from the file. But you can Sometimes you want to check whether somebody written something inside it, right? Ah, which would be this thing. This is how you can actually check right the time that somebody actually written in this thing. And as you can see from this thing, you can see that the last operation last excess operation was actually right. Okay, so we basically written, uh, as they ride was the last operation that we did with this file. Okay, that's pretty cool. And yet another thing that you can do is actually let ask for the length of the file. So how big defile actually ISS, right? Sometimes you want to. It's actually 75 76 bites. So yeah, this is runs again. Pretty cool south. Sometimes you want to, for example, user to be able to create Onley files that have a particular size. Right? So you, for example, can check, But this the length is big or something like that, right? Um, yeah. And last thing that I want to show you is that you can once again check for the name of the file. Not sure. Why would you do it? Because you have the name here, uh, about you. You are able to do it right in the name you can see. Mile takes my text file that dxy. Okay, so yet this is pretty cool stuff that you can do and pretty cool info that you can get about a file. Um, and I'm sure that you will find some use cases in your applications where you can use this stuff. And yes, so that's pretty much it. I hope you enjoy this