In the previous lab we discussed about the C# Exceptions and important things to remember about the Exceptions. Now in this lab we will discuss yet another important topic, and discuss important points related to it. We will discuss about the “system.io” namespace, how to work with files & folders.
Now usually in regular programming we need to sometimes create files, folders, read files, folders, delete files, folders. So basically we have to do lot of operations related to files & folders in our computer’s hard disk. Therefore to do all these operations we will be using a namespace given by .NET called “system.io”where “io” stands for input & output. It means that whatever input output tasks we want to do with our hard disks we are able to do it through this namespace.
Basically we do operations on two things mainly Files & Folders. The reading, creating, deleting operations we do on Files & Folders. So basically whatever operations we want to do with our folders we will have to use “directory” static class. And whatever operations we want to do with our files we will have to use “file” static class. So we will see step by step how to use this “system.io” namespace to do our operations on files & folders.
Step1: The first step is to create a directory or a folder in our computer. The code for doing it is as follows,
Its output is shown as follows,
In the above image we can see how we have created new folder in our computer’s C drive. Now with this directory class we can do other operations on our folder such as move, copy, delete, etc.
Step 2:Here we will create a file in our folder which we have created and write some text into it. Now we can simply create a file by using the command “File.Create”, but it will only create the file we also want to write some text in the file while creating it. So the code for the following is given below,
The output of this will be as follows,
The “NewFile” is created inside our “CreatedNewDirectory” folder in our C drive.
Step 3:Here we will write multiple lines in our text file. The above code was applicable if we are having single line of text. But for multiple lines we have to use some other code which is given below,
The output of it will be as follows,
Here we can see how multiple lines of text are written in our “NewFile”. We do this when we have to write multiple lines of text instead of single line of text. There are two types in writing the text in the file, “WriteAllText” is for single line of text and “WriteAllLines” is for multiple lines of text.
Step 4:Now if we want to browse through all the directories in any of our drive in our computer we have a command for it,
The output for it will be as follows,
Here we can see it has listed down all the directories or Folders in our D drive.
Now if we want to loop through each of these directories or Folders, means to show all the contents inside each of the directories we will write some simple command which is shown below,
Its output will be,
Here we can see how the contents of the directories are shown.
Step 5:Now if we want to read the contents of the file we will be using the file class and select the appropriate action for reading the contents of that file. The command for it is,
The output of this will be,
Here all the contents of the file are read in one go. Now what if we want to display the text in an array and then loop through it, for that we have to make some changes in our command which are as follows,
Here the output looks same because it is reading in one go from the file. In the previous command of “ReadAllText” it was just the reading the contents from the file and displaying it as a single string whereas the command for “ReadLines” reads the contents of the file in an array and then loop through it, so that it can be displayed in grid or wherever we want.
Step 6:Sometimes it happens that we want to read or know about the file info, whether it is a read only file or not, what kind of file it is, which extension the file has, etc. so for that we have a class known as File Info. Its syntaxes are as follows,
Its output is,
It says that the file exists it is True and it has a “.txt” extension.
Step 7: The same as we have File Info class we have something called as Directory Info class also and it helps to get the info of the directory. The command for it is as follows,
Its output is,
Here it says that the directory exists it is True and also it shows the full name of the directory as “c:\CreatedNewDirectory”
Basically these are the 7 important points to remember about the “system.io” namespace.
If you are new to C# language and .NET platform below is a worth watching video covering learning fundamental with practical project: –