DirectoryInfo.Create メソッド

定義

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

オーバーロード

Create()

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

Create(DirectorySecurity)

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

Create()

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

public:
 void Create();
public void Create ();
member this.Create : unit -> unit
Public Sub Create ()

例外

ディレクトリを作成できません。

次の例では、指定したディレクトリが存在するかどうかを確認し、存在しない場合はディレクトリを作成し、ディレクトリを削除します。

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 it 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 );
   }

}
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

ディレクトリに適用するアクセス制御。

例外

呼び出し元に、必要なアクセス許可がありません。

2.1 より前のバージョンの.NET Frameworkと .NET Core: path は長さ 0 の文字列、空白のみを含む、または 1 つ以上の無効な文字を含みます。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

マップされていないドライブにあるなど、指定されたパスが無効です。

コロン (:) 文字だけでディレクトリを作成しようとしました。

次のコード例では、指定したディレクトリ セキュリティ属性を使用して、ユーザーの一時フォルダー内に新しいディレクトリを作成します。

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 タスク を参照してください。

重要

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

適用対象