From the course: C# File System Tips and Tricks

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating a file

Creating a file - C# Tutorial

From the course: C# File System Tips and Tricks

Start my 1-month free trial

Creating a file

- [Instructor] So now that we know how to create folders, let's take a look at how we can create files inside of the folders. We'll scroll down to the bottom of our class and we're going to create a new public method. This will have a return type of void and we're going to call this CreateFile. The first thing we need in order to create a file is a path, so we'll create a new variable called path and we're going to call GetFolderByName. We'll pass in our FolderNames and we're going to get a reference to the SaveData folder path. Next, we're going to want to add a string so that we can give this a file name. Here, we'll call this TestFile.txt and now we have the full path to where we want to save our text file as well as the name of the text file itself. Now in order for us to write this to a folder, we just need to call the File class' static WriteAllText method. Here we'll pass in the path and for the contents, we'll write Hello World. Now let's go to the top of our class and after…

Contents