This documentation is archived and is not being maintained.

Directory.CreateDirectory Method

Creates all directories and subdirectories as specified by path.

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


The directory path to create.

Return Value

A DirectoryInfo as specified by path.


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.


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


Delete a directory. Directory.Delete


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


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


[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"

            ' Determine whether the directory exists.
            If Directory.Exists(path) Then
                Console.WriteLine("That path exists already.")
            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.
            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

using System;
using System.IO;

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

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

            // 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.
            Console.WriteLine("The directory was deleted successfully.");
        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());
        finally {}

#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}.",

        // Delete the directory.
        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:


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


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


[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.


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