Bewerken

Delen via


DirectoryInfo.CreateSubdirectory Method

Definition

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

Overloads

CreateSubdirectory(String)

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

CreateSubdirectory(String, DirectorySecurity)

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

CreateSubdirectory(String)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

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

public:
 System::IO::DirectoryInfo ^ CreateSubdirectory(System::String ^ path);
public System.IO.DirectoryInfo CreateSubdirectory (string path);
member this.CreateSubdirectory : string -> System.IO.DirectoryInfo
Public Function CreateSubdirectory (path As String) As DirectoryInfo

Parameters

path
String

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

Returns

The last directory specified in path.

Exceptions

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.

-or-

A file already has the name specified by path.

The specified path, file name, or both exceed the system-defined maximum length.

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.

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

Examples

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 namespace System;
using namespace System::IO;
int main()
{
   
   // Create a reference to a directory.
   DirectoryInfo^ di = gcnew 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 );
}
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);
    }
}
open System.IO

// Create a reference to a directory.
let di = DirectoryInfo "TempDir"

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

// Create a subdirectory in the directory just created.
let dis = di.CreateSubdirectory "SubDir"

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

// Delete the subdirectory.
dis.Delete true

// Delete the directory.
di.Delete true
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
            di.Create()
        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.
        dis.Delete(True)

        ' Delete the directory.
        di.Delete(True)
    End Sub
End Class

Remarks

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.

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

See also

Applies to

CreateSubdirectory(String, DirectorySecurity)

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

public:
 System::IO::DirectoryInfo ^ CreateSubdirectory(System::String ^ path, System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public System.IO.DirectoryInfo CreateSubdirectory (string path, System.Security.AccessControl.DirectorySecurity directorySecurity);
member this.CreateSubdirectory : string * System.Security.AccessControl.DirectorySecurity -> System.IO.DirectoryInfo
Public Function CreateSubdirectory (path As String, directorySecurity As DirectorySecurity) As DirectoryInfo

Parameters

path
String

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

directorySecurity
DirectorySecurity

The security to apply.

Returns

The last directory specified in path.

Exceptions

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.

-or-

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

The specified path, file name, or both exceed the system-defined maximum length.

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.

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

Remarks

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.

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

Applies to