Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
19 out of 28 rated this helpful - Rate this topic

Directory.CreateDirectory Method

Creates all directories and subdirectories as specified by path.

[Visual Basic]
Public Shared Function CreateDirectory( _
   ByVal path As String _
) As DirectoryInfo
[C#]
public static DirectoryInfo CreateDirectory(
 string path
);
[C++]
public: static DirectoryInfo* CreateDirectory(
 String* path
);
[JScript]
public static function CreateDirectory(
   path : String
) : DirectoryInfo;

Parameters

path
The directory path to create.

Return Value

A DirectoryInfo as specified by path.

Exceptions

Exception Type Condition
IOException The directory specified by path is read-only or is not empty.
UnauthorizedAccessException The caller does not have the required permission.
ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException path is a null reference (Nothing in Visual Basic).
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
NotSupportedException Creating a directory with only the colon (:) character was attempted.

Remarks

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. The path parameter specifies a directory path, not a file path.Creating a directory with only the colon (:) character is not supported, and will cause a NotSupportedException to be thrown.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Delete a directory. Directory.Delete

DirectoryInfo.Delete

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos

Example

[Visual Basic, C#, C++] The following example creates and deletes the specified directory.

[Visual Basic] 
Imports System
Imports System.IO
Imports Microsoft.VisualBasic

Public Class Test
    Public Shared Sub Main()
        ' Specify the directory you want to manipulate.
        Dim path As String = "c:\MyDir"

        Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) Then
                Console.WriteLine("That path exists already.")
                Return
            End If

            ' Try to create the directory.
            Dim di As DirectoryInfo = Directory.CreateDirectory(path)
            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path))

            ' 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

[C#] 
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directory you want to manipulate.
        string path = @"c:\MyDir";

        try 
        {
            // Determine whether the directory exists.
            if (Directory.Exists(path)) 
            {
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            DirectoryInfo di = Directory.CreateDirectory(path);
            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directory you want to manipulate.
    String* path = S"c:\\MyDir";

    try {
        // Determine whether the directory exists.
        if (Directory::Exists(path)) {
            Console::WriteLine(S"That path exists already.");
            return 0;
        }

        // Try to create the directory.
        DirectoryInfo* di = Directory::CreateDirectory(path);
        Console::WriteLine(S"The directory was created successfully at {0}.",
             __box(Directory::GetCreationTime(path)));

        // Delete the directory.
        di->Delete();
        Console::WriteLine(S"The directory was deleted successfully.");
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    } 
}

[Visual Basic, C#, C++] To create the directory C:\Users\User1\Public\Html when the current directory is C:\Users\User1, use any of the following calls to ensure that the backslash is interpreted properly.

[Visual Basic, C#, C++] In Visual Basic:

Directory.CreateDirectory("Public\Html") 
    Directory.CreateDirectory("\Users\User1\Public\Html") 
    Directory.CreateDirectory("c:\Users\User1\Public\Html")

[Visual Basic, C#, C++] In C#:

Directory.CreateDirectory("Public\\Html");
    Directory.CreateDirectory("\\Users\\User1\\Public\\Html");
    Directory.CreateDirectory("c:\\Users\\User1\\Public\\Html");

[Visual Basic, C#, C++] In C++:

Directory::CreateDirectory("Public\\Html");
    Directory::CreateDirectory("\\Users\\User1\\Public\\Html");
    Directory::CreateDirectory("c:\\Users\\User1\\Public\\Html");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

Directory Class | Directory Members | System.IO Namespace | DirectoryInfo | Working with I/O | Reading Text from a File | Writing Text to a File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.