Export (0) Print
Expand All
4 out of 6 rated this helpful - Rate this topic

Directory.CreateDirectory Method (String)

Creates all directories and subdirectories in the specified path unless they already exist.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
public static DirectoryInfo CreateDirectory(
	string path
)

Parameters

path
Type: System.String

The directory to create.

Return Value

Type: System.IO.DirectoryInfo
An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists.
ExceptionCondition
IOException

The directory specified by path is a file.

-or-

The network name is not known.

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. You can query for invalid characters by using the GetInvalidPathChars method.

-or-

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

ArgumentNullException

path is null.

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. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.

The path parameter specifies a directory path, not a file path.

Trailing spaces are removed from the end of the path parameter before creating the directory.

You can create a directory on a remote computer, on a share that you have write access to. UNC paths are supported; for example, you can specify the following for path: \\2009\Archives\December in Visual Basic, and \\\\2009\\Archives\\December in C#.

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

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

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");

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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