Click to Rate and Give Feedback
TechNet
TechNet Library
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
DirectoryInfo..::.Create Method

Creates a directory.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
Public Sub Create
C#
public void Create()
Visual C++
public:
void Create()
F#
member Create : unit -> unit 
ExceptionCondition
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.

Visual Basic
Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
        Try
            ' Determine whether the directory exists.
            If di.Exists Then
                ' Indicate that it already exists.
                Console.WriteLine("That path exists already.")
                Return
            End If

            ' 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 e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
C#
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 {}
    }
}
Visual C++
using namespace System;
using namespace System::IO;
int main()
{

   // Specify the directories you want to manipulate.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\MyDir" );
   try
   {

      // Determine whether the directory exists.
      if ( di->Exists )
      {

         // Indicate that it already exists.
         Console::WriteLine( "That path exists already." );
         return 0;
      }

      // 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 );
   }

}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Documentation clarification      noonand2   |   Edit   |   Show History

It's not made abundantly clear by the documentation but this works in two very useful ways:

1.
Given a path that doesn't exist, it will create all necessary sub-directories as well. If you pass the path "c:\staging\2011\04" to it and only the staging folder exists, this method will create the '2011' and '04' directories.

2.
It works with UNC paths. If you pass the path "\\computer-name\staging\2011\04" to it and only the staging folder exists, this method will create the '2011' and '04' directories.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker