DirectoryInfo.Create メソッド

定義

ディレクトリを作成します。

オーバーロード

名前 説明
Create()

ディレクトリを作成します。

Create(DirectorySecurity)

DirectorySecurity オブジェクトを使用してディレクトリを作成します。

Create()

ディレクトリを作成します。

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

このメソッドは、System.Security.AccessControl アセンブリの一部として、FileSystemAclExtensions クラスの拡張メソッドとして Create(DirectoryInfo, DirectorySecurity) .NET Core 3.1 に移植されました。

適用対象