다음을 통해 공유


DirectoryInfo 클래스

디렉터리 및 하위 디렉터리를 만들고, 이동하고, 열거하는 인스턴스 메서드를 노출합니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.IO
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class DirectoryInfo
    Inherits FileSystemInfo
‘사용 방법
Dim instance As DirectoryInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class DirectoryInfo : FileSystemInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class DirectoryInfo sealed : public FileSystemInfo
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class DirectoryInfo extends FileSystemInfo
SerializableAttribute 
ComVisibleAttribute(true) 
public final class DirectoryInfo extends FileSystemInfo

설명

디렉터리의 복사, 이동, 이름 바꾸기 및 삭제와 같은 일반적인 작업을 수행하는 데 DirectoryInfo 클래스를 사용할 수 있습니다.

개체를 여러 번 다시 사용해야 하는 경우에는 Directory 클래스의 해당하는 정적 메서드 대신 DirectoryInfo 인스턴스 메서드를 사용할 수 있으며, 이 메서드는 보안 검사가 필수적이지 않습니다.

참고

입력 문자열로 경로를 받아들이는 멤버의 경우 해당 경로는 제대로 구성되어야 합니다. 그렇지 않으면 예외가 발생합니다. 예를 들어, 경로가 정규화된 경로이지만 공백으로 시작하면 클래스의 메서드에서 경로가 트리밍되지 않습니다. 따라서 경로가 잘못되고 예외가 발생합니다. 비슷하게 경로나 경로 조합은 두 번 정규화될 수 없습니다. 예를 들어, "c:\temp c:\windows"는 대부분의 경우에 예외를 발생시킵니다. 경로 문자열이 적용되는 메서드를 사용할 경우 해당 경로가 제대로 구성되었는지 확인해야 합니다.

경로가 적용되는 멤버에서 경로는 파일이나 디렉터리를 참조할 수 있습니다. 또한 지정된 경로는 서버와 공유 이름의 상대 경로나 UNC(Universal Naming Convention) 경로를 나타낼 수 있습니다. 예를 들어, 다음과 같은 경로를 사용할 수 있습니다.

  • C#의 "c:\\MyDir\\MyFile.txt" 또는 Visual Basic의 "c:\MyDir\MyFile.txt"

  • C#의 "c:\\MyDir" 또는 Visual Basic의 "c:\MyDir"

  • C#의 "MyDir\\MySubdir" 또는 Visual Basic의 "MyDir\MySubDir"

  • C#의 "\\\\MyServer\\MyShare" 또는 Visual Basic의 "\\MyServer\MyShare"

기본적으로 모든 사용자에게 새 디렉터리에 대한 모든 읽기/쓰기 권한이 부여됩니다.

다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

디렉터리 이름을 바꾸거나 이동합니다.

Directory.Move

DirectoryInfo.MoveTo

디렉터리를 삭제합니다.

Directory.Delete

DirectoryInfo.Delete

디렉터리를 만듭니다.

CreateDirectory

Directory

하위 디렉터리를 만듭니다.

CreateSubdirectory

디렉터리의 파일을 참조하십시오.

Name

디렉터리의 하위 디렉터리를 참조하십시오.

GetDirectories

GetDirectories

디렉터리에 있는 모든 하위 디렉터리와 모든 파일을 참조하십시오.

GetFileSystemInfos

디렉터리의 크기를 찾습니다.

Directory

파일이 있는지 여부를 확인합니다.

Exists

디렉터리의 파일을 크기순으로 정렬합니다.

GetFileSystemInfos

디렉터리가 있는지 여부를 확인합니다.

Exists

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 플랫폼 참고: 장치마다 파일 시스템이 다르게 작동하므로 .NET Compact Framework에서는 디렉터리 특성을 가져오거나 설정할 수 없습니다.

예제

다음 예제에서는 DirectoryInfo 클래스의 일부 주 멤버에 대해 설명합니다.

Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
        Try
            ' Determine whether the directory exists.
            If di.Exists Then
                ' Indicate that it already exists.
                Console.WriteLine("That path exists already.")
                Return
            End If

            ' Try to create the directory.
            di.Create()
            Console.WriteLine("The directory was created successfully.")

            ' Delete the directory.
            di.Delete()
            Console.WriteLine("The directory was deleted successfully.")

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
        try 
        {
            // Determine whether the directory exists.
            if (di.Exists) 
            {
                // Indicate that the directory already exists.
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            di.Create();
            Console.WriteLine("The directory was created successfully.");

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   
   // Specify the directories you want to manipulate.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\MyDir" );
   try
   {
      
      // Determine whether the directory exists.
      if ( di->Exists )
      {
         
         // Indicate that the directory already exists.
         Console::WriteLine( "That path exists already." );
         return 0;
      }
      
      // Try to create the directory.
      di->Create();
      Console::WriteLine( "The directory was created successfully." );
      
      // Delete the directory.
      di->Delete();
      Console::WriteLine( "The directory was deleted successfully." );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo("c:\\MyDir");

        try {
            // Determine whether the directory exists.
            if (di.get_Exists()) {
                // Indicate that the directory already exists.
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            di.Create();
            Console.WriteLine("The directory was created successfully.");

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {
        }
    } //main
} //Test

다음 예제에서는 디렉터리와 그 내용을 복사하는 방법을 보여 줍니다.

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.IO.FileSystemInfo
      System.IO.DirectoryInfo

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

DirectoryInfo 멤버
System.IO 네임스페이스
File
Attributes
Directory 클래스
Path

기타 리소스

파일 및 스트림 I/O
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기
기본 파일 I/O
방법: 새로 만든 데이터 파일 읽기 및 쓰기