내보내기(0) 인쇄
모두 확장
EN
이 콘텐츠는 한국어로 제공되지 않아 영어 버전으로 표시됩니다.

DirectoryInfo.CreateSubdirectory Method

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

[Visual Basic]
Public Function CreateSubdirectory( _
   ByVal path As String _
) As DirectoryInfo
[C#]
public DirectoryInfo CreateSubdirectory(
 string path
);
[C++]
public: DirectoryInfo* CreateSubdirectory(
 String* path
);
[JScript]
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.

Exceptions

Exception Type Condition
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. Paths must be less than 248 characters, and file names must be less than 260 characters.
SecurityException The caller does not have the required permission.

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.

Note   Path names are limited to 248 characters.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Copy a directory. Directory
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 in all subdirectories of a directory. GetFileSystemInfos

Example

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.

[Visual Basic] 
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
            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 'Main
End Class 'CreateSubTest

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Create a reference to a directory.
    DirectoryInfo* di = new DirectoryInfo(S"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(S"SubDir");

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

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

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

[JScript] 
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();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

표시:
© 2014 Microsoft