Directory.CreateDirectory Method (System.IO) (2024)

  • Reference

Definition

Namespace:
System.IO
Assemblies:
mscorlib.dll, System.IO.FileSystem.dll
Assembly:
System.IO.FileSystem.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll

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

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

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.

Exceptions

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

.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 (:).

ArgumentNullException

path is null.

The specified path, file name, or both exceed the system-defined maximum length.

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:\").

Examples

The following example creates and deletes the specified directory:

using namespace System;using namespace System::IO;int 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 0; } // 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 ); }}
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 {} }}
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.IOPublic 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 SubEnd 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");
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

  • DirectoryInfo
  • File and Stream I/O
  • How to: Read Text from a File
  • How to: Write Text to a File

Applies to

CreateDirectory(String, UnixFileMode)

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

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.

Attributes

UnsupportedOSPlatformAttribute

Exceptions

ArgumentException

path is a zero-length string, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

ArgumentNullException

path is null.

ArgumentException

The file mode is invalid.

UnauthorizedAccessException

The caller does not have the required permission.

PathTooLongException

The specified path exceeds the system-defined maximum length.

IOException

path is a file.

DirectoryNotFoundException

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

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.

Exceptions

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

.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 (:).

ArgumentNullException

path is null.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length.

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:\").

Examples

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); } }}
open System.IOopen System.Security.AccessControllet 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.IOImports System.Security.AccessControlModule Module1 Sub Main() 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 SubEnd Module

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.

Applies to

Directory.CreateDirectory Method (System.IO) (2024)
Top Articles
How did the bushpig cross the strait? A great puzzle in African mammal biogeography solved by genomics
The wild pigs of Africa - Africa Geographic
Washu Parking
Canary im Test: Ein All-in-One Überwachungssystem? - HouseControllers
Health Benefits of Guava
Insidious 5 Showtimes Near Cinemark Tinseltown 290 And Xd
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
CKS is only available in the UK | NICE
Noaa Weather Philadelphia
414-290-5379
Everything You Need to Know About Holly by Stephen King
Wisconsin Women's Volleyball Team Leaked Pictures
10 Free Employee Handbook Templates in Word & ClickUp
Arboristsite Forum Chainsaw
Baywatch 2017 123Movies
Louisiana Sportsman Classifieds Guns
Cashtapp Atm Near Me
Snow Rider 3D Unblocked Wtf
Houses and Apartments For Rent in Maastricht
50 Shades Darker Movie 123Movies
Velocity. The Revolutionary Way to Measure in Scrum
Craigslist Maui Garage Sale
20 Different Cat Sounds and What They Mean
Tyler Sis University City
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Joan M. Wallace - Baker Swan Funeral Home
Sef2 Lewis Structure
How to Grow and Care for Four O'Clock Plants
Plaza Bonita Sycuan Bus Schedule
Anotherdeadfairy
Roane County Arrests Today
Piri Leaked
Sensual Massage Grand Rapids
Himekishi Ga Classmate Raw
Guinness World Record For Longest Imessage
Baldur's Gate 3 Dislocated Shoulder
Graphic Look Inside Jeffrey Dresser
Www.craigslist.com Syracuse Ny
Mega Millions Lottery - Winning Numbers & Results
The Pretty Kitty Tanglewood
Federal Student Aid
3400 Grams In Pounds
Hell's Kitchen Valley Center Photos Menu
Craigslist Freeport Illinois
Wasmo Link Telegram
O'reilly's Palmyra Missouri
Ezpawn Online Payment
Quiktrip Maple And West
Television Archive News Search Service
Reilly Auto Parts Store Hours
My Gsu Portal
Gummy Bear Hoco Proposal
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6325

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.