Exportar (0) Imprimir
Expandir todo

DirectoryInfo Class

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)

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

The DirectoryInfo type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360DirectoryInfoWhen it is called by trusted applications, initializes a new instance of the DirectoryInfo class. .
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360AttributesWhen called by trusted applications, gets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CreationTimeWhen called by trusted applications, gets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ExistsGets a value indicating whether the directory exists. (Overrides FileSystemInfo.Exists.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ExtensionGets the string representing the extension part of the file. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360FullNameWhen called by trusted applications, gets the full path of the directory or file. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360LastAccessTimeWhen called by trusted applications, gets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360LastWriteTimeWhen called by trusted applications, gets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NameGets the name of this DirectoryInfo instance. (Overrides FileSystemInfo.Name.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ParentWhen it is called by trusted applications, gets the parent directory of a specified subdirectory.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360RootWhen it is called by trusted applications, gets the root portion of a path.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CreateWhen it is called by trusted applications, creates a directory.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CreateSubdirectoryWhen it is called by trusted applications, creates a subdirectory or subdirectories on the specified path.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Delete()When it is called by trusted applications, deletes this DirectoryInfo if it is empty. (Overrides FileSystemInfo.Delete().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Delete(Boolean)When it is called by trusted applications, deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
Public methodEnumerateDirectories()When it is called by trusted applications, returns an enumerable collection of directory information in the current directory.
Public methodEnumerateDirectories(String)When it is called by trusted applications, returns an enumerable collection of directory information that matches a specified search pattern.
Public methodEnumerateDirectories(String, SearchOption)When it is called by trusted applications, returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.
Public methodEnumerateFiles()When it is called by trusted applications, returns an enumerable collection of file information in the current directory.
Public methodEnumerateFiles(String)When it is called by trusted applications, returns an enumerable collection of file information that matches a search pattern.
Public methodEnumerateFiles(String, SearchOption)When it is called by trusted applications, returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.
Public methodEnumerateFileSystemInfos()When it is called by trusted applications, returns an enumerable collection of file system information in the current directory.
Public methodEnumerateFileSystemInfos(String)When it is called by trusted applications, returns an enumerable collection of file system information that matches a specified search pattern.
Public methodEnumerateFileSystemInfos(String, SearchOption)When it is called by trusted applications, returns an enumerable collection of file system information that matches a specified search pattern and search subdirectory option.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetDirectories()Security Critical. Returns the subdirectories of the current directory.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetDirectories(String)Security Critical. Returns an array of directories in the current DirectoryInfo matching the given search criteria.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetFiles()Security Critical. Returns a file list from the current directory.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetFiles(String)Security Critical. Returns a file list from the current directory matching the given searchPattern.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetFileSystemInfos()Security Critical. Returns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetFileSystemInfos(String)Security Critical. Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories matching the specified search criteria.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MoveToWhen it is called by trusted applications, moves a DirectoryInfo instance and its contents to a new path.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360RefreshRefreshes the state of the object. (Inherited from FileSystemInfo.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns the original path that was passed by the user. (Overrides Object.ToString().)
Top

  NameDescription
Protected fieldSupported by Silverlight for Windows PhoneSupported by Xbox 360FullPathInfrastructure. Represents the fully qualified path of the directory or file. (Inherited from FileSystemInfo.)
Protected fieldSupported by Silverlight for Windows PhoneSupported by Xbox 360OriginalPathInfrastructure. The path originally specified by the user, whether relative or absolute. (Inherited from FileSystemInfo.)
Top

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

This type is present to support the .NET Compact Framework infrastructure in Silverlight for 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<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).

For example code and information about how to create an application that runs outside the browser, see Out-of-Browser Support.


private void LoadImage()
{

	if (Application.Current.HasElevatedPermissions)
	{
		DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(
		   Environment.SpecialFolder.MyPictures));

		var files = from f in di.EnumerateFiles()
					where f.Extension.ToLower() == ".jpg" || 
						f.Extension == ".png"
					select f;

		int max = files.Count() + 1;

		Random rnd = new Random();
		int r = rnd.Next(0, max);

		List<FileInfo> pics = new List<FileInfo>(files);

		string randpic = pics[r].FullName;

		BitmapImage img = new BitmapImage();
		img.SetSource(new FileStream(randpic, FileMode.Open));

		MyImage.Source = img;

	}
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft