DirectoryInfo Class

May 11, 2014

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

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

'Declaration
Public NotInheritable Class DirectoryInfo _
	Inherits FileSystemInfo

The DirectoryInfo type exposes the following members.

  NameDescription
Public methodDirectoryInfoInitializes a new instance of the DirectoryInfo class.
Top

  NameDescription
Public propertyAttributesGets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.)
Public propertyCreationTimeGets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.)
Public propertyExistsGets a value indicating whether the directory exists. (Overrides FileSystemInfo.Exists.)
Public propertyExtensionGets the string representing the extension part of the file. (Inherited from FileSystemInfo.)
Public propertyFullNameGets the full path of the directory or file. (Inherited from FileSystemInfo.)
Public propertyLastAccessTimeGets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.)
Public propertyLastWriteTimeGets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.)
Public propertyNameGets the name of this DirectoryInfo instance. (Overrides FileSystemInfo.Name.)
Public propertyParentGets the parent directory of a specified subdirectory.
Public propertyRootGets the root portion of a path.
Top

  NameDescription
Public methodCreateCreates a directory.
Public methodCreateSubdirectoryCreates a subdirectory or subdirectories on the specified path.
Public methodDeleteDeletes this DirectoryInfo if it is empty. (Overrides FileSystemInfo.Delete.)
Public methodDelete(Boolean)Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetDirectoriesReturns the subdirectories of the current directory.
Public methodGetDirectories(String)Returns an array of directories in the current DirectoryInfo matching the given search criteria.
Public methodGetFilesReturns a file list from the current directory.
Public methodGetFiles(String)Returns a file list from the current directory matching the given searchPattern.
Public methodGetFileSystemInfosReturns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.
Public methodGetFileSystemInfos(String)Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories matching the specified search criteria.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodMoveToMoves a DirectoryInfo instance and its contents to a new path.
Public methodRefreshRefreshes the state of the object. (Inherited from FileSystemInfo.)
Public methodToStringReturns the original path that was passed by the user. (Overrides Object.ToString.)
Top

  NameDescription
Protected fieldFullPathInfrastructure. Represents the fully qualified path of the directory or file. (Inherited from FileSystemInfo.)
Protected fieldOriginalPathInfrastructure. The path originally specified by the user, whether relative or absolute. (Inherited from FileSystemInfo.)
Top

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.

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.

Version Notes

Windows Phone

This type is present to support the .NET Compact Framework infrastructure in Windows Phone, and it is not intended to be used in your application code.

The following example displays an image picked at random from the user's My Pictures folder. It uses the DirectoryInfo class to obtain an enumerable collection of FileInfo objects that represent files that have a .jpg or .png extension. That collection is used to construct a List(Of T) collection so that its index can be used to select a file that corresponds to the random number.

The example then creates a bitmap image by using the FileStream class and sets it as the source for an Image control (named MyImage).


Private Sub LoadImage()

	
		Dim di As New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))

		Dim files = From f In di.EnumerateFiles() _
			Where f.Extension.ToLower() = ".jpg" OrElse f.Extension = ".png" _
			Select f

		Dim max As Integer = files.Count() + 1

		Dim rnd As New Random()
		Dim r As Integer = rnd.[Next](0, max)

		Dim pics As New List(Of FileInfo)(files)

		Dim randpic As String = pics(r).FullName

		Dim img As New BitmapImage()
		img.SetSource(New FileStream(randpic, FileMode.Open))


		MyImage.Source = img
	
End Sub


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Show:
© 2014 Microsoft