In order to change the location of our file, we need to create a path at which we want to store it.
So there are different ways to do it.
I want to show you the one using a verbatim string literal.
So the At sign in C sharp allows us to use those or to denote it.
So I have this string let's say directory path which will just be let's say C at least usually that's
the hard drive right.
Or your main hard drive at least in windows.
And then I want to store it in logs.
Now you see that there is a little bit of a problem here directly.
Why is that.
Because I'm using this backslash.
And the backslash is an escape character in side of a string.
So we saw that with this backslash n here you see it's a different color.
It's like this orange ish yellow orange color.
And this indicates that the next character is a special character and will be treated.
So in this case I'm using a capital L which is not a special character.
And also this backslash should be considered a backslash.
So what you can do is obviously you can use a double backslash.
So it will escape the next character in the sense that it will just say, okay, you know what?
My next character take it literally.
So it takes it literally in this case.
And it just says, okay, I'm going to use this backslash as a backslash.
However, when you have a very long path it becomes quite annoying.
Right.
So you to always do that.
So instead what you can do is you can add an at sign here at the start of the string.
What it will do is it will just say okay, take whatever is inside of it literally.
So don't use any like this backslash.
It's going to be considered as a backslash and not as an escape character.
So the At sign is used to denote a verbatim string literal okay.
So now we have the directory path.
And now let's add the file path that we want to do.
So string dot file path is going to be system dot I o dot.
And here I'm going to use path.
So there's this path class which allows us to combine paths.
So I'm going to say combine whatever the directory path is with the name of the file that I want to
use.
So directory path and then the second path.
And I'm going to call this one log dot txt okay.
This will make a little more sense in a second.
So you see I don't need to use system I o because it is part of my code anyways.
So it's in my directives slash in my namespaces that are added.
You can see that here it is added by default.
And now that means that I can take away this grayed out text.
So find all text.
It needs a string path.
And then this combine method.
You see it returns a string.
So what we can just do here now is we can use our file path here and paste that here.
So now if we run this we will get a new folder.
And inside of our C hard drive which is called logs.
And it will have a text called log text with the text hello world and a line break.
Now if we were to run this we would get an error.
It says directory not found exception.
So what we can do is we can create the directory.
So we can say if the directory doesn't exist.
So directory dot exists.
And here I'm going to use the directory path not the file path.
If the directory path does not exist.
So I'm adding the exclamation mark here.
Then create a directory at the directory path.
So directory dot create directory adds directory path.
So I'm just going to create this folder called logs for us.
And if that worked then go ahead and create this file.
Because this append all text will also generate a file for us.
And now let's run this and we don't get an error.
That's good.
Now let's go into our C folder and we will see that we have this logs folder down here.
And inside of the logs folder we have our text file log text with the message hello world.
Okay so that's pretty cool.
Now we know how to create a file and actually create a directory as well.
It's really cool how C sharp does that in a very little code in my opinion.
Okay.
Now obviously you could go ahead and say string message and then whatever the message is.
So this is a log entry for example, would be a little better than just having a Hello World text every
time.
So let's go ahead and run it again.
And this time we should see that inside of our log dot text file.
Not this one, but the one that we created.
Inside of our C folder logs.
Log dot text has this.
This is a log entry.
Cool.
So that's it for this video.
In the next video we're going to look at decoupling okay.
It will be quite powerful.
So see you there.