Export (0) Print
Expand All

Directory.CreateDirectory Method (String)

Creates all directories and subdirectories as specified by path.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static DirectoryInfo CreateDirectory (
	string path
)
public static DirectoryInfo CreateDirectory (
	String path
)
public static function CreateDirectory (
	path : String
) : DirectoryInfo
Not applicable.

Parameters

path

The directory path to create.

Return Value

A DirectoryInfo as specified by path.

Exception typeCondition

IOException

The directory specified by path is read-only.

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.

-or-

path is prefixed with, or contains only a colon character (:).

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 (for example, it is on an unmapped drive).

NotSupportedException

path contains a colon character (:) that is not part of a drive label ("C:\").

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. If the directory already exists, this method does nothing.

Creating a directory with only the colon (:) character is not supported, and will cause a NotSupportedException to be thrown.

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

The following code example creates and deletes the specified directory.

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

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        // 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 (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {
        }
    } //main
} //Test

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.

In Visual Basic:

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

In C#:

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

In C++:

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft