DirectoryInfo.Create 메서드

정의

디렉터리를 만듭니다.

오버로드

Create()

디렉터리를 만듭니다.

Create(DirectorySecurity)

DirectorySecurity 개체를 사용하여 디렉터리를 만듭니다.

Create()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

디렉터리를 만듭니다.

public void Create ();

예외

디렉터리를 만들 수 없습니다.

예제

다음 예제에서는 지정된 디렉터리가 있는지 확인하고, 디렉터리가 없는 경우 디렉터리를 만들고, 디렉터리를 삭제합니다.

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 it 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 {}
    }
}

설명

의 일부가 path 유효하지 않은 경우 에 path 지정된 모든 디렉터리를 만듭니다. 매개 변수는 path 파일 경로가 아닌 디렉터리 경로를 지정합니다. 디렉터리가 이미 있는 경우 이 메서드는 아무 것도 수행하지 않습니다. 이 메서드를 호출하기 전에 디렉터리가 없으면 디렉터리에 대한 캐시된 특성 정보가 만들어지면 플러시됩니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

.NET 9 및 기타 버전
제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Create(DirectorySecurity)

DirectorySecurity 개체를 사용하여 디렉터리를 만듭니다.

public void Create (System.Security.AccessControl.DirectorySecurity directorySecurity);

매개 변수

directorySecurity
DirectorySecurity

디렉터리에 적용할 액세스 제어입니다.

예외

호출자에게 필요한 권한이 없는 경우

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars() 메서드를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

path이(가) null인 경우

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

콜론(:) 문자만으로 디렉터리를 만들려고 했습니다.

예제

다음 코드 예제에서는 지정된 디렉터리 보안 특성을 사용하여 사용자의 임시 폴더 내에 새 디렉터리를 만듭니다.

using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            DirectorySecurity security = new DirectorySecurity();
            SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
            FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
            security.AddAccessRule(accessRule);
            string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            dirInfo.Create(security);
        }
    }
}

설명

이 메서드 오버로드를 사용하여 액세스 제어가 있는 디렉터리를 만들 수 있으므로 보안이 적용되기 전에 디렉터리에 액세스할 수 있는 기회가 없습니다.

디렉터리가 이미 있는 경우 이 메서드는 아무 것도 수행하지 않습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

중요

이 메서드는 어셈블리Create(DirectoryInfo, DirectorySecurity)System.Security.AccessControl 일부로 클래스의 FileSystemAclExtensions 확장 메서드인 .NET Core 3.1로 이식되었습니다.

적용 대상

.NET Framework 4.8.1 및 기타 버전
제품 버전
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1