DirectoryInfo.Create DirectoryInfo.Create DirectoryInfo.Create DirectoryInfo.Create Method

Definition

Creates a directory.

Overloads

Create() Create() Create() Create()

Creates a directory.

Create(DirectorySecurity) Create(DirectorySecurity) Create(DirectorySecurity)

Creates a directory using a DirectorySecurity object.

Create() Create() Create() Create()

Creates a directory.

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

Exceptions

The directory cannot be created.

Examples

The following example checks whether a specified directory exists, creates the directory if it does not exist, and deletes the directory.

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

Remarks

If the directory already exists, this method does nothing.

For a list of common I/O tasks, see Common I/O Tasks.

Security

FileIOPermission
for writing files. Associated enumeration: Write

See also

Create(DirectorySecurity) Create(DirectorySecurity) Create(DirectorySecurity)

Creates a directory using a DirectorySecurity object.

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

Parameters

directorySecurity
DirectorySecurity DirectorySecurity DirectorySecurity DirectorySecurity

The access control to apply to the directory.

Exceptions

The directory specified by path is read-only or is not empty.

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

The specified path, file name, or both exceed the system-defined maximum length.

The specified path is invalid, such as being on an unmapped drive.

Creating a directory with only the colon (:) character was attempted.

Remarks

Use this method overload to create a directory with access control, so there is no chance the directory can be accessed before security is applied.

If the directory already exists, this method does nothing.

For a list of common I/O tasks, see Common I/O Tasks.

Security

FileIOPermission
for permission to create a directory. Security action: Demand. Associated enumeration: Read

Applies to