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.CreateSubdirectory Method (String)

Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

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

public DirectoryInfo CreateSubdirectory (
	string path
)
public DirectoryInfo CreateSubdirectory (
	String path
)
public function CreateSubdirectory (
	path : String
) : DirectoryInfo

Parameters

path

The specified path. This cannot be a different disk volume or Universal Naming Convention (UNC) name.

Return Value

The last directory specified in path.

Exception typeCondition

ArgumentException

path does not specify a valid file path or contains invalid DirectoryInfo characters.

ArgumentNullException

path is a null reference (Nothing in Visual Basic).

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

The subdirectory cannot be created.

-or-

A file or directory already has the name specified by path.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path, file name, or both are too long.

SecurityException

The caller does not have code access permission to create the directory.

-or-

The caller does not have code access permission to read the directory described by the returned DirectoryInfo object. This can occur when the path parameter describes an existing directory.

Any and all directories specified in path are created, unless some part of path is invalid. The path parameter specifies a directory path, not a file path. If the subdirectory already exists, this method does nothing.

NoteNote

Path names are limited to 248 characters.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Rename or move a directory.

Directory.Move

DirectoryInfo.MoveTo

Delete a directory.

Directory.Delete

DirectoryInfo.Delete

Create a directory.

CreateDirectory

Directory

See the files in a directory.

Name

See the subdirectories of a directory.

GetDirectories

GetDirectories

See all the files and all the subdirectories in a directory.

GetFileSystemInfos

The following example demonstrates creating a subdirectory. In this sample, the created directories are removed once created. Therefore, to test this sample, comment out the delete lines in the code.

using System;
using System.IO;

public class CreateSubTest 
{
    public static void Main() 
    {
        // Create a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        // ...

        // Delete the subdirectory.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    }
}

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

public class CreateSubTest
{
    public static void main(String[] args)
    {
        // Create a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.get_Exists() == false) {
            di.Create();
        }

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        //...
        // Delete the subdirectory.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    } //main
} //CreateSubTest

import System;
import System.IO;

public class CreateSubTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        // ...

        // Delete the subdirectory.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    }
}

CreateSubTest.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft