DirectoryInfo Constructor
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| path is a null reference (Nothing in Visual Basic). |
|
| The caller does not have the required permission. |
|
| path contains invalid characters such as ", <, >, or |. |
|
| 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 |
|---|
| 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. |
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. | |
| Write to a text file. | |
| Read from a text file. | |
| Rename or move a directory. | |
| Delete a directory. | |
| Create a directory. | |
| Create a subdirectory. | |
| See the files in a directory. | |
| See the subdirectories of a directory. | |
| See all the files and all the subdirectories in a directory. | |
| Find the size of a directory. | |
| Determine if a file exists. | |
| Sort files in a directory by size. | GetFileSystemInfos |
| Determine if a directory exists. |
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
- FileIOPermission for reading from files and directories. Associated enumeration: FileIOPermissionAccess.Read
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Caution