DirectoryInfo.Create Method
Creates a directory.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| IOException | The directory cannot be created. |
If the directory already exists, this method does nothing.
For a list of common I/O tasks, see Common I/O Tasks.
The following example checks whether a specified directory exists, creates the directory if it does not exist, and deletes the directory.
using System; using System.IO; class Test { public static void Main() { // Specify the directories you want to manipulate. DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); try { // Determine whether the directory exists. if (di.Exists) { // Indicate that it already exists. Console.WriteLine("That path exists already."); return; } // Try to create the directory. di.Create(); Console.WriteLine("The directory was created successfully."); // Delete the directory. di.Delete(); Console.WriteLine("The directory was deleted successfully."); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally {} } }
- FileIOPermission
for writing files. Associated enumeration: FileIOPermissionAccess.Write
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.