DirectoryInfo Class
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class DirectoryInfo sealed : public FileSystemInfo
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public final class DirectoryInfo extends FileSystemInfo
SerializableAttribute ComVisibleAttribute(true) public final class DirectoryInfo extends FileSystemInfo
Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class, because a security check will not always be necessary.
![]() |
---|
In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string. |
In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:
-
"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.
-
"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.
-
"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.
-
"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.
By default, full read/write access to new directories is granted to all users.
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. | Directory |
Determine if a file exists. | |
Sort files in a directory by size. | GetFileSystemInfos |
Determine if a directory exists. |
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: Because file systems for devices operate differently, the .NET Compact Framework does not support getting or setting directory attributes.
The following example demonstrates some of the main members of the DirectoryInfo class.
using namespace System; using namespace System::IO; int main() { // Specify the directories you want to manipulate. DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\MyDir" ); try { // Determine whether the directory exists. if ( di->Exists ) { // Indicate that the directory already exists. Console::WriteLine( "That path exists already." ); return 0; } // Try to create the directory. di->Create(); Console::WriteLine( "The directory was created successfully." ); // Delete the directory. di->Delete(); Console::WriteLine( "The directory was deleted successfully." ); } catch ( Exception^ e ) { Console::WriteLine( "The process failed: {0}", e ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { // Specify the directories you want to manipulate. DirectoryInfo di = new DirectoryInfo("c:\\MyDir"); try { // Determine whether the directory exists. if (di.get_Exists()) { // Indicate that the directory already exists. Console.WriteLine("That path exists already."); return; } // Try to create the directory. di.Create(); Console.WriteLine("The directory was created successfully."); // Delete the directory. di.Delete(); Console.WriteLine("The directory was deleted successfully."); } catch (System.Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally { } } //main } //Test
The following example demonstrates how to copy a directory and its contents.
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.