Heim  >  Artikel  >  Backend-Entwicklung  >  C# DirectoryInfo

C# DirectoryInfo

WBOY
WBOYOriginal
2024-09-03 15:17:59497Durchsuche

C# Directoryinfo allow us to deal with directory folders system, DirectoryInfo is a class which is available inside the System.IO or simply the namespace System.IO contains the DirectoryInfo class, DirectoryInfo class contains almost the similar feature of the FileInfo class of C#, the only difference that the DirectoryInfo only focus on the Directory not on the file systems, when we talk about the DirectoryInfo class we always talking about the physical directory and with the help of it, we get the object with which we can create, delete, and also we can make some subdirectory and many more operations can be performed.

Syntax

Below is the simple syntax for the implementation of the DirectoryInfo class. We can explain the below syntax in the following ways.

  • First, we have defined a class with a variable with a type of DirectoryInfo.
  • We are assigning the object created by DirectoryInfo with help of a new keyword.
  • We can see the syntax here we are passing the dpath for the object creation to the DirectoryInfo class.
  • Here dpath is any string of path.
  • Finally, we are using the code as the directory.create, and it will create the directory.
  • Remember that we should also check for whether a directory already exists or not.
//sourceDirectory: This is the string of the path or directory for which we want to perform certain operations.
DirectoryInfo directory = new DirectoryInfo(dPath);
directory.Create();

Working of C# DirectoryInfo class

We can explain the working of the DirectoryInfo class in the following way.

  • Namespace Sytem.IO contains the class the DirectoryInfo, so if we want to use it we need to include this library.
  • Most important thing about it, by using the available command we can create and move the directory.
  • It has many methods which are the key strength of the DirectoryInfo, which allows us to perform creation and deletion.
  • Most important point about the DirectoryInfo class is that we cannot inherit it because it is a sealed class (we can learn more about the sealed class in C# in its documentation).
  • Inheritance flow of the DirectoryInfo class is Object ===> MarshalByRefObject ===> FileSystemInfo ===> DirectoryInfo ,this dependency show how they have inherited from its parent to the DirectoryInfo class.

Constructors of C# DirectoryInfo

In the constructors are the way to initialization of the DirectoryInfo class. Here we need to pass the path to initialize, and the path is the string of directory which we want to create or move.

Function type ( private/public/protected ) DirectoryInfo ( string directoryPath );

Attribut,

directoryPath: This is the string path for which we are calling the constructor for creating an object for the given path to perform a move or create operation.

Methods of C# DirectoryInfo

Here are the following methods mention below:

  • Create ( string ): If we want to create a new directory we can use the method. Here in the method, we are passing a string which string path for which we want to create the directory.
  • CreateSubdirectory: We learned that we can create the directory with the help of the method create, now what if we wanted to create a directory inside another directory ( subdirectory ). We can simply use the CreateSubdirectory method for it. Bypassing a string path to this method we can also create a subdirectory to the specified path.
  • MoveTo: It used to move all the constants and the instances of the directory to the other location.
  • Delete: It will delete the specified directory, bypassing a boolean value to it we can inform its compiler if we want to delete its subdirectory also.
  • GetDirectories: To know about the subdirectory we can use this method. Many times in real life programming where we need to know the pathname before deleting, so it will be very useful as it mentions the subdirectory details.
  • GetFiles: In case if we want to get the file from the specified directory then we can use the GetFile method.
  • GetType(): To know the type of instance ( current ).
  • Refresh(): To refresh the object state we can use the method Refresh().
  • SetAccessControl: This method is mostly used for security reasons and it will get a DirectorySecurity as the object to describe it.
  • ToString(): To get the original path that was passed by the user we can use the method ToString().

Properties of C# DirectoryInfo

Here are the properties mention below

  • CreationTime: In case if we wanted to know the date and time of the directory creation then we can use the property CreationTime.
  • Exists: It returns the boolean value, which shows if the directory exists or not. In case if the directory is already there then it returns true ele it will return false.
  • FullName: If we wanted to get the full name of the file ( which means starting from root directory ).
  • Name: In this case, it used simply to get the name of the directory.
  • LastAccessTime: In case if we wanted to get the last date and time when the directory was modified then we can use this property.
  • LastWriteTime: If we wanted to get the last file changes and save the details of the changes.
  • Extension: It is used to get the string representing the extension part of the file.
  • Parent: In case if we wanted to get the parent directory name then we can use Parent. It will give us the parent directory name.

Example of C# DirectoryInfo

Below is a very simple example, here we are simply trying to create a directory, we are also checking if the directory already exists or not.

Please see the below example along with the screen of output.

Code:

using System.IO;
using System;
class createDirectory
{
static void Main()
{
string dPath = @"D:\directoryExample";
//Initialisation of the Object by passing the path
DirectoryInfo directory = new DirectoryInfo(dPath);
// Let us first check if the directory already exist or not
if (directory.Exists)
{
Console.WriteLine("The directory which you are trying to create is already there");
}
//If the directory which we are trying to create is not there
else
{
// below code will create the directory with name we have provided
directory.Create();
Console.WriteLine("Congratulation we have created directory");
}
Console.ReadLine();
}
}

Output:

C# DirectoryInfo

Conclusion

From this tutorial, we learned about the DirectoryInfo in C# and we learned about the DirectoryInfo behaviors with a very important example. We learned about the constructors and methods of the DirectoryInfo. We understand the working of DirectoryInfo in C#.

Das obige ist der detaillierte Inhalt vonC# DirectoryInfo. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:C# Enthält()Nächster Artikel:C# Enthält()