Directory.CreateDirectory Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates all the directories in a specified path.
Overloads
| CreateDirectory(String) |
Creates all directories and subdirectories in the specified path unless they already exist. |
| CreateDirectory(String, UnixFileMode) |
Creates all directories and subdirectories in the specified path with the specified permissions unless they already exist. |
| CreateDirectory(String, DirectorySecurity) |
Creates all the directories in the specified path, unless they already exist, applying the specified Windows security. |
CreateDirectory(String)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
Creates all directories and subdirectories in the specified path unless they already exist.
public:
static System::IO::DirectoryInfo ^ CreateDirectory(System::String ^ path);
public static System.IO.DirectoryInfo CreateDirectory(string path);
static member CreateDirectory : string -> System.IO.DirectoryInfo
Public Shared Function CreateDirectory (path As String) As DirectoryInfo
Parameters
- path
- String
The directory to create.
Returns
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.
Exceptions
The caller does not have the required permission.
.NET Framework and .NET Core versions older than 2.1: 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 (:).
path is null.
The specified path, file name, or both exceed the system-defined maximum length.
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:\").
Examples
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";
// Determine whether the directory exists.
if (Directory.Exists(path))
{
Console.WriteLine("That path exists already.");
return;
}
DirectoryInfo di;
try
{
// Try to create the directory.
di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("The caller does not have the required permission to create `{0}`", path);
return;
}
// Delete the directory.
di.Delete();
Console.WriteLine("The directory was deleted successfully.");
}
}
open System.IO
// Specify the directory you want to manipulate.
let path = @"c:\MyDir"
try
// Determine whether the directory exists.
if Directory.Exists path then
printfn "That path exists already."
else
// Try to create the directory.
let di = Directory.CreateDirectory path
printfn $"The directory was created successfully at {Directory.GetCreationTime path}."
// Delete the directory.
di.Delete()
printfn "The directory was deleted successfully."
with e ->
printfn $"The process failed: {e}"
Imports System.IO
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
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:
Directory.CreateDirectory("Public\Html")
Directory.CreateDirectory("\Users\User1\Public\Html")
Directory.CreateDirectory("c:\Users\User1\Public\Html")
Directory.CreateDirectory("Public\\Html");
Directory.CreateDirectory("\\Users\\User1\\Public\\Html");
Directory.CreateDirectory("c:\\Users\\User1\\Public\\Html");
Remarks
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.
On Unix systems, use a forward slash (/) as path separator.
See also
Applies to
CreateDirectory(String, UnixFileMode)
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
- Source:
- Directory.cs
Creates all directories and subdirectories in the specified path with the specified permissions unless they already exist.
public:
static System::IO::DirectoryInfo ^ CreateDirectory(System::String ^ path, System::IO::UnixFileMode unixCreateMode);
[System.Runtime.Versioning.UnsupportedOSPlatform("windows")]
public static System.IO.DirectoryInfo CreateDirectory(string path, System.IO.UnixFileMode unixCreateMode);
[<System.Runtime.Versioning.UnsupportedOSPlatform("windows")>]
static member CreateDirectory : string * System.IO.UnixFileMode -> System.IO.DirectoryInfo
Public Shared Function CreateDirectory (path As String, unixCreateMode As UnixFileMode) As DirectoryInfo
Parameters
- path
- String
The directory to create.
- unixCreateMode
- UnixFileMode
A bitwise combination of the enumeration values that specifies the Unix file mode used to create directories.
Returns
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.
- Attributes
Exceptions
path is a zero-length string, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.
path is null.
The file mode is invalid.
The caller does not have the required permission.
The specified path exceeds the system-defined maximum length.
path is a file.
A component of the path is not a directory.
Applies to
CreateDirectory(String, DirectorySecurity)
Creates all the directories in the specified path, unless they already exist, applying the specified Windows security.
public:
static System::IO::DirectoryInfo ^ CreateDirectory(System::String ^ path, System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public static System.IO.DirectoryInfo CreateDirectory(string path, System.Security.AccessControl.DirectorySecurity directorySecurity);
static member CreateDirectory : string * System.Security.AccessControl.DirectorySecurity -> System.IO.DirectoryInfo
Public Shared Function CreateDirectory (path As String, directorySecurity As DirectorySecurity) As DirectoryInfo
Parameters
- path
- String
The directory to create.
- directorySecurity
- DirectorySecurity
The access control to apply to the directory.
Returns
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.
Exceptions
The caller does not have the required permission.
.NET Framework and .NET Core versions older than 2.1: 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 (:).
path is null.
The specified path, file name, or both exceed the system-defined maximum length.
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:\").
Examples
The following example creates a new directory with access rules for two user accounts.
using System;
using System.IO;
using System.Security.AccessControl;
partial class Program
{
static void DirectorySecurityExample()
{
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);
}
}
open System.IO
open System.Security.AccessControl
let securityRules = DirectorySecurity()
securityRules.AddAccessRule(FileSystemAccessRule(@"Domain\account1", FileSystemRights.Read, AccessControlType.Allow))
securityRules.AddAccessRule(FileSystemAccessRule(@"Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow))
let di = Directory.CreateDirectory(@"C:\destination\NewDirectory", securityRules)
Imports System.IO
Imports System.Security.AccessControl
Partial Class Program
Shared Sub DirectorySecurityExample()
Dim securityRules As DirectorySecurity = New DirectorySecurity()
securityRules.AddAccessRule(New FileSystemAccessRule("Domain\account1", FileSystemRights.Read, AccessControlType.Allow))
securityRules.AddAccessRule(New FileSystemAccessRule("Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow))
Dim di As DirectoryInfo = Directory.CreateDirectory("C:\destination\NewDirectory", securityRules)
End Sub
End Class
Remarks
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.