DirectoryInfo.Create 메서드

정의

디렉터리를 만듭니다.

오버로드

Name Description
Create()

디렉터리를 만듭니다.

Create(DirectorySecurity)

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

Create()

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

디렉터리를 만듭니다.

public:
 void Create();
public void Create();
member this.Create : unit -> unit
Public Sub 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 {}
    }
}
open System.IO

// Specify the directories you want to manipulate.
let di = DirectoryInfo @"c:\MyDir"

try
    // Determine whether the directory exists.
    if di.Exists then
        // Indicate that it already exists.
        printfn "That path exists already."
    else
        // Try to create the directory.
        di.Create()
        printfn "The directory was created successfully."

        // Delete the directory.
        di.Delete()
        printfn "The directory was deleted successfully."
with e ->
    printfn $"The process failed: {e}"
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

설명

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

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

추가 정보

적용 대상

Create(DirectorySecurity)

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

public:
 void Create(System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public void Create(System.Security.AccessControl.DirectorySecurity directorySecurity);
member this.Create : System.Security.AccessControl.DirectorySecurity -> unit
Public Sub Create (directorySecurity As DirectorySecurity)

매개 변수

directorySecurity
DirectorySecurity

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

예외

호출자에게 필요한 권한이 없습니다.

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

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

콜론(:) 문자만 있는 디렉터리 만들기를 시도했습니다.

예제

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

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 작업을 참조하세요.

Important

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

적용 대상