Export (0) Print
Expand All

DirectoryInfo Constructor

Initializes a new instance of the DirectoryInfo class on the specified path.

[Visual Basic]
Public Sub New( _
   ByVal path As String _
)
[C#]
public DirectoryInfo(
   string path
);
[C++]
public: DirectoryInfo(
   String* path
);
[JScript]
public function DirectoryInfo(
   path : String
);

Parameters

path
A string specifying the path on which to create the DirectoryInfo.

Exceptions

Exception Type Condition
ArgumentNullException path is a null reference (Nothing in Visual Basic).
SecurityException The caller does not have the required permission.
ArgumentException path contains invalid characters such as ", <, >, or |.
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.

Remarks

This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations.

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

CAUTION   When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

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...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
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

Create a subdirectory. CreateSubdirectory
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
Find the size of a directory. Directory
Determine if a file exists. Exists
Sort files in a directory by size. GetFileSystemInfos
Determine if a directory exists. Exists

Example

[Visual Basic, C#, C++] The following example uses this constructor to create the specified directory and subdirectory and demonstrates that a directory that contains subdirectories cannot be deleted.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di1 As DirectoryInfo = New DirectoryInfo("c:\MyDir")
        Dim di2 As DirectoryInfo = New DirectoryInfo("c:\MyDir\temp")
        Try
            ' Create the directories.
            di1.Create()
            di2.Create()
            ' This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", di1.Name)
            di1.Delete()
            Console.WriteLine("The Delete operation was successful, which was unexpected.")
        Catch e As Exception
            Console.WriteLine("The Delete operation failed as expected.")
        End Try
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di1 = new DirectoryInfo(@"c:\MyDir");
        DirectoryInfo di2 = new DirectoryInfo(@"c:\MyDir\temp");

        try 
        {
            // Create the directories.
            di1.Create();
            di2.Create();

            // This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}.", di1.Name);
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, which was unexpected.");
        } 
        catch (Exception) 
        {
            Console.WriteLine("The Delete operation failed as expected.");
        } 
        finally {}
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Specify the directories you want to manipulate.
    DirectoryInfo* di1 = new DirectoryInfo(S"c:\\MyDir");
    DirectoryInfo* di2 = new DirectoryInfo(S"c:\\MyDir\\temp");

    try {
        // Create the directories.
        di1->Create();
        di2->Create();

        // This operation will not be allowed because there are subdirectories.
        Console::WriteLine(S"I am about to attempt to delete {0}.", di1->Name);
        di1->Delete();
        Console::WriteLine(S"The Delete operation was successful, which was unexpected.");
    } catch (Exception*) {
        Console::WriteLine(S"The Delete operation failed as expected.");
    } 
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

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

Show:
© 2014 Microsoft