>  기사  >  백엔드 개발  >  C# 디렉토리정보

C# 디렉토리정보

WBOY
WBOY원래의
2024-09-03 15:17:59353검색

C# Directoryinfo를 사용하면 디렉터리 폴더 시스템을 처리할 수 있습니다. DirectoryInfo는 System.IO 내부에서 사용할 수 있는 클래스이거나 간단히 System.IO 네임스페이스에 DirectoryInfo 클래스가 포함되어 있고, DirectoryInfo 클래스에는 FileInfo 클래스와 거의 유사한 기능이 포함되어 있습니다. C#에서는 DirectoryInfo가 파일 시스템이 아닌 디렉터리에만 초점을 맞춘다는 점에서 DirectoryInfo 클래스에 대해 이야기할 때 항상 물리적 디렉터리에 대해 이야기하고 이를 사용하여 생성할 수 있는 개체를 얻습니다. 삭제하고 하위 디렉토리를 만들어 더 많은 작업을 수행할 수도 있습니다.

구문

다음은 DirectoryInfo 클래스 구현을 위한 간단한 구문입니다. 아래 구문은 다음과 같은 방법으로 설명할 수 있습니다.

  • 먼저 DirectoryInfo 유형의 변수를 사용하여 클래스를 정의했습니다.
  • 새 키워드를 사용하여 DirectoryInfo에서 생성된 개체를 할당하고 있습니다.
  • 여기에서 개체 생성을 위한 dpath를 DirectoryInfo 클래스에 전달하는 구문을 볼 수 있습니다.
  • 여기서 dpath는 임의의 경로 문자열입니다.
  • 마지막으로 코드를 디렉터리 생성으로 사용하면 디렉터리가 생성됩니다.
  • 디렉토리가 이미 존재하는지 여부도 확인해야 한다는 점을 기억하세요.
//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();

C# DirectoryInfo 클래스 작업

DirectoryInfo 클래스의 작동을 다음과 같이 설명할 수 있습니다.

  • 네임스페이스 Sytem.IO에는 DirectoryInfo 클래스가 포함되어 있으므로 이를 사용하려면 이 라이브러리를 포함해야 합니다.
  • 가장 중요한 점은 사용 가능한 명령을 사용하여 디렉토리를 생성하고 이동할 수 있다는 것입니다.
  • 다양한 메소드가 있는데 이는 DirectoryInfo의 핵심 강점으로 생성과 삭제를 수행할 수 있게 해줍니다.
  • DirectoryInfo 클래스의 가장 중요한 점은 봉인된 클래스이기 때문에 상속할 수 없다는 것입니다. C#의 봉인된 클래스에 대한 자세한 내용은 해당 문서에서 확인할 수 있습니다.
  • DirectoryInfo 클래스의 상속 흐름은 Object ===> MarshalByRefObject ===> 파일시스템정보 ===> DirectoryInfo, 이 종속성은 상위 클래스에서 DirectoryInfo 클래스로 상속된 방법을 보여줍니다.

C# DirectoryInfo 생성자

생성자에는 DirectoryInfo 클래스를 초기화하는 방법이 있습니다. 여기에서 초기화할 경로를 전달해야 하며, 경로는 생성하거나 이동하려는 디렉터리의 문자열입니다.

함수 유형(private/public/protected) DirectoryInfo(문자열 디렉토리 경로);

속성

directoryPath: 이동 또는 생성 작업을 수행하기 위해 지정된 경로에 대한 객체를 생성하기 위한 생성자를 호출하는 문자열 경로입니다.

C# DirectoryInfo 메서드

아래에 언급된 방법은 다음과 같습니다.

  • Create ( string ): 새 디렉토리를 생성하려면 이 메소드를 사용할 수 있습니다. 여기 메소드에서는 디렉토리를 생성하려는 문자열 경로를 전달합니다.
  • CreateSubdirectory: create 메소드를 사용하여 디렉토리를 생성할 수 있다는 것을 배웠습니다. 이제 다른 디렉토리(하위 디렉토리) 내에 디렉토리를 생성하려면 어떻게 해야 할까요? CreateSubdirectory 메서드를 사용하면 됩니다. 이 메소드에 대한 문자열 경로를 우회하면 지정된 경로에 대한 하위 디렉토리를 생성할 수도 있습니다.
  • MoveTo: 디렉터리의 모든 상수와 인스턴스를 다른 위치로 이동하는 데 사용됩니다.
  • 삭제: 부울 값을 우회하여 지정된 디렉토리를 삭제합니다. 하위 디렉토리도 삭제하려는 경우 컴파일러에 알릴 수 있습니다.
  • GetDirectories: 하위 디렉토리에 대해 알기 위해 이 방법을 사용할 수 있습니다. 실제 프로그래밍에서는 삭제하기 전에 경로 이름을 알아야 하는 경우가 많으므로 하위 디렉터리 세부 정보를 언급하므로 매우 유용할 것입니다.
  • GetFiles: 지정된 디렉터리에서 파일을 가져오려는 경우 GetFile 메서드를 사용할 수 있습니다.
  • GetType(): 인스턴스 유형(현재)을 확인합니다.
  • Refresh(): 객체 상태를 새로 고치려면 Refresh() 메서드를 사용할 수 있습니다.
  • SetAccessControl: 이 메서드는 주로 보안상의 이유로 사용되며 이를 설명하는 개체로 DirectorySecurity를 ​​가져옵니다.
  • ToString(): 사용자가 전달한 원래 경로를 얻으려면 ToString() 메서드를 사용할 수 있습니다.

C# DirectoryInfo의 속성

아래에 언급된 숙소는 다음과 같습니다

  • 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# 디렉토리정보

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#.

위 내용은 C# 디렉토리정보의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 포함()다음 기사:C# 포함()