Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

DirectoryInfo.DirectoryInfo Constructor

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public DirectoryInfo (
	string path
)
public DirectoryInfo (
	String path
)
public function DirectoryInfo (
	path : String
)
Not applicable.

Parameters

path

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

Exception typeCondition

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.

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 noteCaution:

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 a list of common I/O tasks, see Common I/O Tasks.

The following example uses this constructor to create the specified directory and subdirectory, and demonstrates that a directory that contains subdirectories cannot be deleted.

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 {}
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        // 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.get_Name());
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, "
                + "which was unexpected.");
        }
        catch (System.Exception exp) {
            Console.WriteLine("The Delete operation failed as expected.");
        }
        finally {
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.