CreateSubdirectory Method (String)

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 Function CreateSubdirectory (
	path As String
) As DirectoryInfo


Type: System.String

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

Return Value

Type: System.IO.DirectoryInfo

The last directory specified in path.

Exception Condition

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


path is null.


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


The subdirectory cannot be created.


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


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.


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


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.


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.


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.

Imports System
Imports System.IO

Public Class CreateSubTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")

        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")

        ' Process that directory as required.
        ' ...

        ' Delete the subdirectory.

        ' Delete the directory.
    End Sub 'Main
End Class 'CreateSubTest

for reading and writing files. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft