Export (0) Print
Expand All

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
Not applicable.

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.

NotSupportedException

path contains a colon character (:) that is not part of a drive label ("C:\").

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.

For a list of common I/O tasks, see Common I/O Tasks.

The following example demonstrates creating a subdirectory. In this example, 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 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

ADD
Show:
© 2014 Microsoft