[Serializable] |
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 of the following are acceptable paths:
By default, full read/write access to new directories is granted to all users.
ctor #1 | Initializes a new instance of the DirectoryInfo class on the specified path. |
Attributes (inherited from System.IO.FileSystemInfo) |
Read-write See base class member description: System.IO.FileSystemInfo.Attributes Gets or sets the FileAttributes of the current FileSystemInfo. |
CreationTime (inherited from System.IO.FileSystemInfo) |
Read-write See base class member description: System.IO.FileSystemInfo.CreationTime Gets or sets the creation time of the current FileSystemInfo object. |
Exists | Read-only Overridden: Gets a value indicating whether the directory exists. |
Extension (inherited from System.IO.FileSystemInfo) |
Read-only See base class member description: System.IO.FileSystemInfo.Extension Gets the string representing the extension part of the file. |
FullName (inherited from System.IO.FileSystemInfo) |
Read-only See base class member description: System.IO.FileSystemInfo.FullName Gets the full path of the directory or file. |
LastAccessTime (inherited from System.IO.FileSystemInfo) |
Read-write See base class member description: System.IO.FileSystemInfo.LastAccessTime Gets or sets the time the current file or directory was last accessed. |
LastWriteTime (inherited from System.IO.FileSystemInfo) |
Read-write See base class member description: System.IO.FileSystemInfo.LastWriteTime Gets or sets the time when the current file or directory was last written to. |
Name | Read-only Overridden: Gets the name of this DirectoryInfo instance. |
Parent | Read-only Gets the parent directory of a specified subdirectory. |
Root | Read-only Gets the root portion of a path. |
Create | Creates a directory. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
CreateSubdirectory | Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class. |
Delete | Overloaded:Delete() Overridden: Deletes this DirectoryInfo if it is empty. |
Delete | Overloaded:Delete(bool recursive) Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetDirectories | Overloaded:GetDirectories() Returns the subdirectories of the current directory. |
GetDirectories | Overloaded:GetDirectories(string searchPattern) Returns an array of directories in the current DirectoryInfo matching the given search criteria. |
GetFiles | Overloaded:GetFiles() Returns a file list from the current directory. |
GetFiles | Overloaded:GetFiles(string searchPattern) Returns a file list from the current directory matching the given searchPattern, such as "*.txt". |
GetFileSystemInfos | Overloaded:GetFileSystemInfos() Returns an array of strongly typed FileSystemInfo entries listing all the files and directories. |
GetFileSystemInfos | Overloaded:GetFileSystemInfos(string searchPattern) Retrieves an array of strongly typed FileSystemInfo objects matching the specified search criteria. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
MoveTo | Moves a DirectoryInfo instance and its contents to a new path. |
Refresh (inherited from System.IO.FileSystemInfo) |
See base class member description: System.IO.FileSystemInfo.Refresh Refreshes the state of the object. |
ToString | Overridden: Returns the original path that was passed by the user. |
FullPath (inherited from System.IO.FileSystemInfo) |
See base class member description: System.IO.FileSystemInfo.FullPath Represents the fully qualified path of the directory or file. |
OriginalPath (inherited from System.IO.FileSystemInfo) |
See base class member description: System.IO.FileSystemInfo.OriginalPath The path originally specified by the user, whether relative or absolute. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public DirectoryInfo( |
path
Exception Type | Condition |
---|---|
ArgumentNullException | path is null. |
DirectoryNotFoundException | The path is not found. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path contains invalid characters such as ", <, >, or |. |
PathTooLongException | The specified path, file name, or both are too long. After full qualification, each must contain fewer than 256 characters. |
protected string FullPath;
|
protected string OriginalPath;
|
public FileAttributes Attributes {get; set;}
|
Exception Type | Condition |
---|---|
FileNotFoundException | The caller attempts to access a file that does not exist on disk. |
SecurityException | The caller does not have the required permission. |
ArgumentException | The caller attempts to set an invalid file attribute. |
IOException | FileSystemInfo.Refresh cannot initialize the data. |
public DateTime CreationTime {get; set;}
|
Exception Type | Condition |
---|---|
IOException | FileSystemInfo.Refresh cannot initialize the data. |
FileNotFoundException | The FileSystemInfo object does not exist. |
public override bool Exists {get;}
|
static public void CopyDirectory(string SourceDirectory, string TargetDirectory) { DirectoryInfo source = new DirectoryInfo(SourceDirectory); DirectoryInfo target = new DirectoryInfo(TargetDirectory); //Check If we have valid source if(!source.Exists) return; if(!target.Exists) target.Create(); //Copy Files FileInfo[] sourceFiles = source.GetFiles(); for(int i = 0; i < sourceFiles.Length; ++i) File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name,true); //Copy directories DirectoryInfo[] sourceDirectories = source.GetDirectories(); for(int j = 0; j < sourceDirectories.Length; ++j) CopyDirectory(sourceDirectories[j].FullName,target.FullName +"\\" + sourceDirectories[j].Name); }
public string Extension {get;}
|
public virtual string FullName {get;}
|
public DateTime LastAccessTime {get; set;}
|
Exception Type | Condition |
---|---|
IOException | FileSystemInfo.Refresh cannot initialize the data. |
public DateTime LastWriteTime {get; set;}
|
Exception Type | Condition |
---|---|
IOException | FileSystemInfo.Refresh cannot initialize the data. |
public override string Name {get;}
|
using System; using System.IO; // This code outputs the name of the current DirectoryInfo only. class GetAName { public static void Main(string[] args) { DirectoryInfo dir = new DirectoryInfo("."); String dirName=dir.Name; Console.WriteLine("DirectoryInfo name is {0}", dirName); } }
public DirectoryInfo Parent {get;}
|
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
using System; using System.IO; public class MoveToTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("TempDir"); // create the directory, only if it doesn't exist if (di.Exists == false) di.Create(); // create a subdirectory in the directory just created DirectoryInfo dis = di.CreateSubdirectory("SubDir"); // get a reference to the parent directory of the subdir we just made DirectoryInfo parentDir = dis.Parent; Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name); // delete the major directory di.Delete(true); } }
public DirectoryInfo Root {get;}
|
using System; using System.IO; public class MoveToTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("TempDir"); // create the directory, only if it doesn't exist if (di.Exists == false) di.Create(); // create a subdirectory in the directory just created DirectoryInfo dis = di.CreateSubdirectory("SubDir"); Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name, dis.Root); // delete the major directory di.Delete(true); } }
public void Create(); |
If the directory already exists, this method does nothing.
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public DirectoryInfo CreateSubdirectory( |
path
Exception Type | Condition |
---|---|
ArgumentException | path does not specify a valid file path or contains invalid DirectoryInfo characters. |
ArgumentNullException | path is null. |
DirectoryNotFoundException | Part of the path is not found. |
IOException | A file or directory already has the name specified by path. |
PathTooLongException | The specified path, file name, or both are too long. After full qualification, each must be less than 256 characters. |
SecurityException | The caller does not have the required permission. |
By default, full read/write access to new directories is granted to all users.
using System; using System.IO; public class CreateSubTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("TempDir"); // create the directory, only if it doesn't exist if (di.Exists == false) di.Create(); // create a subdirectory in the directory just created DirectoryInfo dis = di.CreateSubdirectory("SubDir"); // process that directory as required // ... // delete the subdirectory dis.Delete(true); // delete the directory di.Delete(true); } }
public override void Delete(); |
Exception Type | Condition |
---|---|
IOException | The directory is not empty. |
SecurityException | The caller does not have the required permission. |
public void Delete( |
recursive
Exception Type | Condition |
---|---|
IOException | The DirectoryInfo is read-only, or the DirectoryInfo contains one or more files or subdirectories and recursive is false. |
SecurityException | The caller does not have the required permission. |
using System; using System.IO; public class DeleteTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("TempDir"); // create the directory, only if it doesn't exist if (di.Exists == false) di.Create(); // create a subdirectory in the directory just created DirectoryInfo dis = di.CreateSubdirectory("SubDir"); // process that directory as required // ... // delete the subdirectory. The true indicates that if subdirectories // or files are in this directory, delete them as well dis.Delete(true); // delete the directory di.Delete(true); } }
~DirectoryInfo(); |
public DirectoryInfo[] GetDirectories(); |
Exception Type | Condition |
---|---|
DirectoryNotFoundException | The path encapsulated in the DirectoryInfo object does not exist. |
using System; using System.IO; public class GetDirectoriesTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("c:\\"); // get a reference to each directory in that directory DirectoryInfo[] diArr = di.GetDirectories(); // print out the names of the directories foreach (DirectoryInfo dri in diArr) Console.WriteLine(dri.Name); } }
public DirectoryInfo[] GetDirectories( |
searchPattern
Exception Type | Condition |
---|---|
ArgumentNullException | searchPattern is null. |
DirectoryNotFoundException | The path encapsulated in the Directory object does not exist. |
SecurityException | The caller does not have the required permission. |
The string ".." can only be used in searchPattern if it is specified as a part of a valid directory name, such as in the directory name "a..b". It cannot be used to move up the directory hierarchy.
If there are no subdirectories, or no subdirectories are found that match searchPattern, this method returns only the root directory.
public FileInfo[] GetFiles(); |
Exception Type | Condition |
---|---|
DirectoryNotFoundException | The directory does not exist. |
using System; using System.IO; public class GetFilesTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("c:\\"); // get a reference to each file in that directory FileInfo[] fiArr = di.GetFiles(); // print out the names of the files foreach (FileInfo fri in fiArr) Console.WriteLine(fri.Name); } }
public FileInfo[] GetFiles( |
searchPattern
Exception Type | Condition |
---|---|
ArgumentNullException | searchPattern is null. |
DirectoryNotFoundException | The directory does not exist. |
SecurityException | The caller does not have the required permission. |
public FileSystemInfo[] GetFileSystemInfos(); |
public FileSystemInfo[] GetFileSystemInfos( |
searchPattern
Exception Type | Condition |
---|---|
ArgumentNullException | searchPattern is null. |
SecurityException | The caller does not have the required permission. |
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
public void MoveTo( |
destDirName
Exception Type | Condition |
---|---|
ArgumentNullException | destDirName is null. |
ArgumentException | destDirName is an empty string (''"). |
IOException | An attempt was made to move a directory to a different volume, or destDirName already exists. |
SecurityException | The caller does not have the required permission. |
This method permits moving a directory to a read-only directory. The read/write attribute of neither directory is affected.
using System; using System.IO; public class MoveToTest { public static void Main() { // make a reference to a directory DirectoryInfo di = new DirectoryInfo("TempDir"); // create the directory, only if it doesn't exist if (di.Exists == false) di.Create(); // create a subdirectory in the directory just created DirectoryInfo dis = di.CreateSubdirectory("SubDir"); // move the main directory. Note that it's contents move also if (Directory.Exists("NewTempDir") == false) di.MoveTo("NewTempDir"); try { // attempt to delete the subdirectory. Note that because it has been // moved, an exception will be thrown dis.Delete(true); } catch (Exception) { // handle this exception. This is for demonstration however. // Console.WriteLine("That directory did not exist"); } // point our directoryinfo reference to the new directory //di = new DirectoryInfo("NewTempDir"); // delete the directory //di.Delete(true); } }
public void Refresh(); |
Exception Type | Condition |
---|---|
ArgumentException | The file is not found. |
IOException | A device such as a disk drive is not ready. |
Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated.
public override string ToString(); |