Information
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.

DirectoryInfo.Create Method ()

Creates a directory.

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

public void Create ()
public void Create ()
public function Create ()
Not applicable.

Exception typeCondition

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 {}
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo("c:\\MyDir");

        try {
            // Determine whether the directory exists.
            if (di.get_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 (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft