.NET Framework class library
DirectoryInfo class
An instance method for creating, moving, and enumerating directories and subdirectories is disclosed.
For a list of all members of this type, see the DirectoryInfo member.
System.Object System.MarshalByrefObject System.IO.Filesysteminfo System.io.DirectoryInfo
[Visual Basic]
Notinheritable Public Class DirectoryInfo
Inherits FileSystemInfo
[C #]
[Serializable]
Public Sealed Class DirectoryInfo: FileSystemInfo
[C ]
[Serializable]
Public __gc __sealed Class DirectoryInfo: Public FileSystemInfo: PUBLIC FileSystemInfo
[Jscript]
public
Serializable
Class DirectoryInfo Extends FileSystemInfo
Thread security
All public static (shared in Visual Basic) is a thread safe. But not to ensure that any instance member is thread safe.
Note
Use the DirectoryInfo class for typical operations such as copy, move, rename, create, and delete the directory.
If you intend to reuse an object multiple times, consider using the DirectoryInfo instance method, not the corresponding static method of the Directory class, because it does not always need a security check.
Note that in a member of the accepted path as an input string, the format of the path must be correct, otherwise an exception will be triggered. For example, if the path is fully limited but at the beginning, the path is not trunked in the class method. Therefore, the format of the path is incorrect and will trigger an exception. Similarly, the combination of paths or paths cannot be completely limited twice. For example, "C: / Temp C: / Windows" will also cause exceptions in most cases. When using the method of accepting a path string, make sure the path is formatted correctly.
In a member of the accepted path, the path can be a file or is only a directory. The specified path can also be a unified naming agreement (UNC) path of a relative path or server and a shared name. For example, the following is an acceptable path:
"C: //mydir/myfile.txt" in C #, "c: /mydir/myfile.txt" in Visual Basic. "C: // mydir" or "c: / mydir" in Visual Basic in C #. "MyDir // mysubdir" or "mydir / mysubdir" in Visual Basic. "// myserver / myshare" in "MyServer // myshare" or Visual Basic in C #.
By default, full read / write access to the new directory is granted to all users.
For examples of this class, see the "Example" section below. The following table lists examples of other typical or related I / O tasks.
To do this ... see Examples in this topic ... Create a text file.
Write the text to the file
Write a text file.
Write the text to the file
Read text files.
Read text from the file
Copy the directory. Directory renamed or moved directories. Directory.Move DirectoryInfo.moveto Delete the directory. Directory.Delete DirectoryInfo.delete creates a directory. CreateDirectory Directory creates a subdirectory. CreateSubDirectory View files in the directory. Name View the subdirectory of the directory. GetDirectories getDirectories View all files in all subdirements of the directory. GetFileSysteminfos View directory size. Directory determines if the file exists. EXISTS is sorted by the size of the file in the directory. GetFileSystemInfos determines if the directory exists. Exists.NET Framework Lite Platform Description: None Suppose or Set Directory Properties.
Example
[Visual Basic, C #, C ] The following example demonstrates some of the main members of the DirectoryInfo class.
[Visual Basic]
Imports system
Imports system.io
Public Class Test
Public Shared Sub Main ()
'Specify The Directories you want to manipulate.
DIM DI As DirectoryInfo = New DirectoryInfo ("c: / mydir")
Try
'DETERMINE WHENER THE DIRECTORY EXISTS.
IF di.exists then
'Indicate That IT Already Exists.
Console.writeline ("That path exissrs already)
Return
END IF
'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 e as exception
Console.writeline ("The Process Failed: {0}", E.TOSTRING ())
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 Di = New 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;
}
// Try to create the Directory.
Di.create ();
Console.writeline ("The Directory Was Created SuccessFully.");
// delete the Directory.
Di.delete ();
"" The Directory Was Deleted Success, ");
Catch (Exception E)
{
Console.writeline ("The Process Failed: {0}", E.TOString ());
}
Finally {}
}
}
[C ]
#using
Using Namespace System;
Using namespace system :: io;
Int main () {
// Specify The Directories you want to manipulate.
DirectoryInfo * Di = New DirectoryInfo (s "c: // mydir");
Try {
// determine WHETHER THE DIRECTORY EXISTS.
IF (di-> exists) {
// Indicate That The Directory Already Exists.
Console :: WriteLine (s "That path exists already.");
Return 0;
}
// Try to create the Directory.
Di-> CREATE ();
Console :: WriteLine (s "The Directory Was Created Success,");
// delete the Directory.
Di-> delete ();
Console :: WriteLine (s "The Directory Was Deleded Success,");
} catch (exception * e) {
Console :: WriteLine (s "The process failed: {0}", e);
}
}
[JScript] There is no example available for JScript. To view Visual Basic, C # or C examples, click the "Language Filter" button in the upper left corner.
Claim
Namespace: system.io
Platform: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series, .NET Framework Lite
Accessure: Mscorlib (in Mscorlib.dll)
See
DirectoryInfo Member | System.io Namespace | File | Attributes | Directory | PATH |
Use I / O
|
Read text from the file
|
Write the text to the file
|
Basic file I / O
|
Read and write data files you just created