Windows apps
Collapse the table of content
Expand the table of content
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.

Directory.CreateDirectory Method (String, DirectorySecurity)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.

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

public static DirectoryInfo CreateDirectory(
	string path,
	DirectorySecurity directorySecurity


Type: System.String

The directory to create.

Type: System.Security.AccessControl.DirectorySecurity

The access control to apply to the directory.

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.

Exception Condition

The directory specified by path is a file.


The network name is not known.


The caller does not have the required permission.


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.


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


path is null.


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.


The specified path is invalid (for example, it is on an unmapped drive).


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

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.

Any and all directories specified in the path parameter 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 not create a new directory, but it returns a DirectoryInfo object for the existing directory.

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 causes a NotSupportedException to be thrown.

The following example creates a new directory with access rules for two user accounts.

using System;
using System.IO;
using System.Security.AccessControl;

namespace ConsoleApplication
    class Program
        static void Main(string[] args)
            DirectorySecurity securityRules = new DirectorySecurity();
            securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\account1", FileSystemRights.Read, AccessControlType.Allow));
            securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow));

            DirectoryInfo di = Directory.CreateDirectory(@"C:\destination\NewDirectory", securityRules);


for reading and writing files and directories and for access to the destination directory. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write Security action: Demand

.NET Framework
Available since 2.0
Return to top
© 2018 Microsoft