DirectoryInfo Class

 

Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

To browse the .NET Framework source code for this type, see the Reference Source.

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

System::Object
  System::MarshalByRefObject
    System.IO::FileSystemInfo
      System.IO::DirectoryInfo

[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class DirectoryInfo sealed : FileSystemInfo

NameDescription
System_CAPS_pubmethodDirectoryInfo(String^)

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

NameDescription
System_CAPS_pubpropertyAttributes

Gets or sets the attributes for the current file or directory.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyCreationTime

Gets or sets the creation time of the current file or directory.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyCreationTimeUtc

Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyExists

Gets a value indicating whether the directory exists.(Overrides FileSystemInfo::Exists.)

System_CAPS_pubpropertyExtension

Gets the string representing the extension part of the file.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyFullName

Gets the full path of the directory or file.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyLastAccessTime

Gets or sets the time the current file or directory was last accessed.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyLastAccessTimeUtc

Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyLastWriteTime

Gets or sets the time when the current file or directory was last written to.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyLastWriteTimeUtc

Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.(Inherited from FileSystemInfo.)

System_CAPS_pubpropertyName

Gets the name of this DirectoryInfo instance.(Overrides FileSystemInfo::Name.)

System_CAPS_pubpropertyParent

Gets the parent directory of a specified subdirectory.

System_CAPS_pubpropertyRoot

Gets the root portion of the directory.

NameDescription
System_CAPS_pubmethodCreate()

Creates a directory.

System_CAPS_pubmethodCreate(DirectorySecurity^)

Creates a directory using a DirectorySecurity object.

System_CAPS_pubmethodCreateObjRef(Type^)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodCreateSubdirectory(String^)

Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

System_CAPS_pubmethodCreateSubdirectory(String^, DirectorySecurity^)

Creates a subdirectory or subdirectories on the specified path with the specified security. The specified path can be relative to this instance of the DirectoryInfo class.

System_CAPS_pubmethodDelete()

Deletes this DirectoryInfo if it is empty.(Overrides FileSystemInfo::Delete().)

System_CAPS_pubmethodDelete(Boolean)

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

System_CAPS_pubmethodEnumerateDirectories()

Returns an enumerable collection of directory information in the current directory.

System_CAPS_pubmethodEnumerateDirectories(String^)

Returns an enumerable collection of directory information that matches a specified search pattern.

System_CAPS_pubmethodEnumerateDirectories(String^, SearchOption)

Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.

System_CAPS_pubmethodEnumerateFiles()

Returns an enumerable collection of file information in the current directory.

System_CAPS_pubmethodEnumerateFiles(String^)

Returns an enumerable collection of file information that matches a search pattern.

System_CAPS_pubmethodEnumerateFiles(String^, SearchOption)

Returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.

System_CAPS_pubmethodEnumerateFileSystemInfos()

Returns an enumerable collection of file system information in the current directory.

System_CAPS_pubmethodEnumerateFileSystemInfos(String^)

Returns an enumerable collection of file system information that matches a specified search pattern.

System_CAPS_pubmethodEnumerateFileSystemInfos(String^, SearchOption)

Returns an enumerable collection of file system information that matches a specified search pattern and search subdirectory option.

System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetAccessControl()

Gets a DirectorySecurity object that encapsulates the access control list (ACL) entries for the directory described by the current DirectoryInfo object.

System_CAPS_pubmethodGetAccessControl(AccessControlSections)

Gets a DirectorySecurity object that encapsulates the specified type of access control list (ACL) entries for the directory described by the current DirectoryInfo object.

System_CAPS_pubmethodGetDirectories()

Returns the subdirectories of the current directory.

System_CAPS_pubmethodGetDirectories(String^)

Returns an array of directories in the current DirectoryInfo matching the given search criteria.

System_CAPS_pubmethodGetDirectories(String^, SearchOption)

Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

System_CAPS_pubmethodGetFiles()

Returns a file list from the current directory.

System_CAPS_pubmethodGetFiles(String^)

Returns a file list from the current directory matching the given search pattern.

System_CAPS_pubmethodGetFiles(String^, SearchOption)

Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.

System_CAPS_pubmethodGetFileSystemInfos()

Returns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.

System_CAPS_pubmethodGetFileSystemInfos(String^)

Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories that match the specified search criteria.

System_CAPS_pubmethodGetFileSystemInfos(String^, SearchOption)

Retrieves an array of FileSystemInfo objects that represent the files and subdirectories matching the specified search criteria.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetObjectData(SerializationInfo^, StreamingContext)

Sets the SerializationInfo object with the file name and additional exception information.(Inherited from FileSystemInfo.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodMoveTo(String^)

Moves a DirectoryInfo instance and its contents to a new path.

System_CAPS_pubmethodRefresh()

Refreshes the state of the object.(Inherited from FileSystemInfo.)

System_CAPS_pubmethodSetAccessControl(DirectorySecurity^)

Applies access control list (ACL) entries described by a DirectorySecurity object to the directory described by the current DirectoryInfo object.

System_CAPS_pubmethodToString()

Returns the original path that was passed by the user.(Overrides Object::ToString().)

System_CAPS_noteNote

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

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.

System_CAPS_noteNote

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.

For a list of common I/O tasks, see Common I/O Tasks.

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 );
   }

}

The following example demonstrates how to copy a directory and its contents.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: