Listado de la etiqueta: Info


The Directory Info class is derived from the System.IO namespace in which the functions related to directories and subdirectories are defined. Directories and subdirectories can be created, renamed, moved, copied, and deleted by using this class’s methods. This class cannot be inherited as enclosed or private, so it is accessed by creating an object.

Directory Info Class

As discussed earlier, the Directory Info Class is used to perform all sorts of functions on the directories and subdirectories in the C# programming language. This class is part of the System.IO namespace, which is why it gives all the input and output functions to the directories and satisfies the namespace’s inheritance.

There are many methods in the Directory Info Class that can be performed on directories and subdirectories in the C# programming language, and several of these methods will be discussed and implemented in the Ubuntu 20.04 environment.

Method 01: Create() Function of Directory Info Class in Ubuntu 20.04

In this method, we will be using the Create() function to create a new directory on our devices in a C# program. The Create() function is a part of the Directory Info class.

Graphical user interface, text Description automatically generated

In the above C# code, we are initializing a string variable that will store the name and the location of a new Directory that we will be creating. Then we will create an object of the Directory Info Class and pass the string variable with the name and the location of the Directory in the object’s parameter. After that, the object will be used to call the Create() function, which will create a new directory on our desktop and print a success message on the command line terminal.

After execution of the above program, we will get the below output:

A screenshot of a computer Description automatically generated with medium confidence

In the output screen, we can see the success message of a new directory creation; we will verify this by locating this directory on our device as shown below:

Graphical user interface, application Description automatically generated

Method 02: Creating a Subdirectory by Using the Directory Info Class in Ubuntu 20.04

In this approach, we will use the Create Subdirectory function in a C# program to create a new subdirectory on our devices. Subdirectories are directories within directories. This method is part of the Directory Info class.

Text Description automatically generated

The following C# code creates a string variable that will contain the name and location of an existing directory in which a new subdirectory will be created. Then we will build a Directory Info Class object and pass the string variable with the Directory’s name and location as a parameter. The object is then used to invoke the “Create subdirectory” method, which creates a new subdirectory within an existing directory. On the command line terminal, we will also print a success message, as demonstrated below:

We can observe the success message of a new subdirectory creation in the output screen above; we can verify this by looking for it in our existing directory, as shown below:

Graphical user interface, application Description automatically generated

Method 03: Getting the Number of Directories by Using the Get Directories Function in Ubuntu 20.04

In this method, we will be writing a program through which we will get the number of directories present on our device. For this method, we will be using the Directory Info class’s function called “Get Directories.”

In the above C# program, we will first create an object of the Directory Info class. Then we will create a variable from the directory info class, which will store the value of the Get Directories function by using the object of the class. Then we will print this value with the help of the Length function as an extension to the variable that we created before.

Graphical user interface, text Description automatically generated

As we can see, the output shows the number of directories on our devices, which is one.

Method 04: Using the Directory Info Class to Get the Number of Files in a Directory in Ubuntu 20.04

In this method, we will use two functions together to calculate the number of files, the Get Directories and Get Files functions.

Text Description automatically generated

In this C# program, we will be creating an object of the Directory Info class with the name and address of the Directory. Then we will call the Get Directories function by initializing a new variable. After this, we will start a for each loop which will move along the directory and store the number of files using the Get Files function with the “Length” extension, and this will be printed on the output screen.

As we can see in the output, the name and complete path of the directory are shown, along with the number of files in the directory.

Method 05: Deleting a Directory by Using the Delete() Function in Ubuntu 20.04

In this method, we will be implementing a function that will delete a directory from our device present in the Directory Info class. The Delete() function is used for deleting a directory in the C# programming language. This function is called with an object of its inherent class.

Graphical user interface Description automatically generated with low confidence

In the above C# program, we will first initialize a string variable that stores the address and the name of the directory file. Then we will create an object of the Directory Info class with the previous string variable as the parameter of the class. After this, we will use the object to call the Delete() function, which will delete the directory present at the location mentioned in the string variable. Then at the end of the program, there will be a success message representing that the method successfully deleted a directory.

Text Description automatically generated with medium confidence

After compiling and executing this C# program, we will get the above output. We can see in the result that we have effectively deleted a directory from our device.

Method 06: Moving a Directory Into Another Directory Using MoveTo() Function in Ubuntu 20.04

In this method, we will be moving a directory into another directory on our device by using a function of the Directory Info class called the MoveTo() function. The directory that we are moving to another directory will become a subdirectory of the existent directory.

Text Description automatically generated

In this C# code, we will be initializing two string variables with the name and location of the directories we need for this scenario. Then we will create two different objects of the Directory Info class for both the directories. Then we will use the MoveTo() function with the object of one directory and pass the string variable that stores the location and name of the other directory. In the end, we will also add a success message which will be visible on the output screen after the program is executed successfully, which can also be seen in the snippet below:

Text Description automatically generated

Conclusion

In this article, we discussed the Directory Info class present in the Systems.IO namespace of the C# programming language. The purpose of the Directory Info class was discussed in this article. We also discussed the methods of the Directory Info that alter directories and subdirectories in C# programming, and we implemented several of these examples in the Ubuntu 20.04 environment to make the use of this class clear.



Source link