mscorlib(4.0.0.0) API with additions
DirectoryInfo.cs
1 using Microsoft.Win32;
5 using System.Security;
8 
9 namespace System.IO
10 {
12  [Serializable]
13  [ComVisible(true)]
14  public sealed class DirectoryInfo : FileSystemInfo
15  {
16  private string[] demandDir;
17 
20  public override string Name => GetDirName(FullPath);
21 
24  public override string FullName
25  {
26  [SecuritySafeCritical]
27  get
28  {
29  Directory.CheckPermissions(string.Empty, FullPath, checkHost: true, FileSecurityStateAccess.PathDiscovery);
30  return FullPath;
31  }
32  }
33 
34  internal override string UnsafeGetFullName
35  {
36  [SecurityCritical]
37  get
38  {
39  Directory.CheckPermissions(string.Empty, FullPath, checkHost: false, FileSecurityStateAccess.PathDiscovery);
40  return FullPath;
41  }
42  }
43 
47  public DirectoryInfo Parent
48  {
49  [SecuritySafeCritical]
50  get
51  {
52  string text = FullPath;
53  if (text.Length > 3 && text.EndsWith(Path.DirectorySeparatorChar))
54  {
55  text = FullPath.Substring(0, FullPath.Length - 1);
56  }
57  string directoryName = Path.GetDirectoryName(text);
58  if (directoryName == null)
59  {
60  return null;
61  }
62  DirectoryInfo directoryInfo = new DirectoryInfo(directoryName, junk: false);
63  Directory.CheckPermissions(string.Empty, directoryInfo.FullPath, checkHost: true, FileSecurityStateAccess.Read | FileSecurityStateAccess.PathDiscovery);
64  return directoryInfo;
65  }
66  }
67 
71  public override bool Exists
72  {
73  [SecuritySafeCritical]
74  get
75  {
76  try
77  {
78  if (_dataInitialised == -1)
79  {
80  Refresh();
81  }
82  if (_dataInitialised != 0)
83  {
84  return false;
85  }
86  return _data.fileAttributes != -1 && (_data.fileAttributes & 0x10) != 0;
87  }
88  catch
89  {
90  return false;
91  }
92  }
93  }
94 
98  public DirectoryInfo Root
99  {
100  [SecuritySafeCritical]
101  get
102  {
103  int rootLength = Path.GetRootLength(FullPath);
104  string text = FullPath.Substring(0, rootLength);
105  string fullPath = Directory.GetDemandDir(text, thisDirOnly: true);
106  FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, fullPath, checkForDuplicates: false, needFullPath: false);
107  return new DirectoryInfo(text);
108  }
109  }
110 
119  [SecuritySafeCritical]
120  public DirectoryInfo(string path)
121  {
122  if (path == null)
123  {
124  throw new ArgumentNullException("path");
125  }
126  Init(path, checkHost: true);
127  }
128 
129  [SecurityCritical]
130  private void Init(string path, bool checkHost)
131  {
132  if (path.Length == 2 && path[1] == ':')
133  {
134  OriginalPath = ".";
135  }
136  else
137  {
138  OriginalPath = path;
139  }
140  string text = FullPath = Directory.GetFullPathAndCheckPermissions(path, checkHost);
141  base.DisplayPath = GetDisplayName(OriginalPath, FullPath);
142  }
143 
144  internal DirectoryInfo(string fullPath, bool junk)
145  {
146  OriginalPath = Path.GetFileName(fullPath);
147  FullPath = fullPath;
148  base.DisplayPath = GetDisplayName(OriginalPath, FullPath);
149  }
150 
151  [SecurityCritical]
152  private DirectoryInfo(SerializationInfo info, StreamingContext context)
153  : base(info, context)
154  {
155  Directory.CheckPermissions(string.Empty, FullPath, checkHost: false);
156  base.DisplayPath = GetDisplayName(OriginalPath, FullPath);
157  }
158 
172  public DirectoryInfo CreateSubdirectory(string path)
173  {
174  if (path == null)
175  {
176  throw new ArgumentNullException("path");
177  }
178  return CreateSubdirectory(path, null);
179  }
180 
195  [SecuritySafeCritical]
196  public DirectoryInfo CreateSubdirectory(string path, DirectorySecurity directorySecurity)
197  {
198  return CreateSubdirectoryHelper(path, directorySecurity);
199  }
200 
201  [SecurityCritical]
202  private DirectoryInfo CreateSubdirectoryHelper(string path, object directorySecurity)
203  {
204  string path2 = Path.InternalCombine(FullPath, path);
205  string fullPathInternal = Path.GetFullPathInternal(path2);
206  if (string.Compare(FullPath, 0, fullPathInternal, 0, FullPath.Length, StringComparison.OrdinalIgnoreCase) != 0)
207  {
208  string displayablePath = __Error.GetDisplayablePath(base.DisplayPath, isInvalidPath: false);
209  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSubPath", path, displayablePath));
210  }
211  string fullPath = Directory.GetDemandDir(fullPathInternal, thisDirOnly: true);
212  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPath, checkForDuplicates: false, needFullPath: false);
213  Directory.InternalCreateDirectory(fullPathInternal, path, directorySecurity);
214  return new DirectoryInfo(fullPathInternal);
215  }
216 
219  public void Create()
220  {
221  Directory.InternalCreateDirectory(FullPath, OriginalPath, null, checkHost: true);
222  }
223 
236  public void Create(DirectorySecurity directorySecurity)
237  {
238  Directory.InternalCreateDirectory(FullPath, OriginalPath, directorySecurity, checkHost: true);
239  }
240 
249  {
251  }
252 
267  {
268  return Directory.GetAccessControl(FullPath, includeSections);
269  }
270 
277  public void SetAccessControl(DirectorySecurity directorySecurity)
278  {
279  Directory.SetAccessControl(FullPath, directorySecurity);
280  }
281 
291  public FileInfo[] GetFiles(string searchPattern)
292  {
293  if (searchPattern == null)
294  {
295  throw new ArgumentNullException("searchPattern");
296  }
297  return InternalGetFiles(searchPattern, SearchOption.TopDirectoryOnly);
298  }
299 
312  public FileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
313  {
314  if (searchPattern == null)
315  {
316  throw new ArgumentNullException("searchPattern");
317  }
318  if (searchOption != 0 && searchOption != SearchOption.AllDirectories)
319  {
320  throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
321  }
322  return InternalGetFiles(searchPattern, searchOption);
323  }
324 
325  private FileInfo[] InternalGetFiles(string searchPattern, SearchOption searchOption)
326  {
327  IEnumerable<FileInfo> collection = FileSystemEnumerableFactory.CreateFileInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
328  List<FileInfo> list = new List<FileInfo>(collection);
329  return list.ToArray();
330  }
331 
335  public FileInfo[] GetFiles()
336  {
337  return InternalGetFiles("*", SearchOption.TopDirectoryOnly);
338  }
339 
346  {
347  return InternalGetDirectories("*", SearchOption.TopDirectoryOnly);
348  }
349 
359  public FileSystemInfo[] GetFileSystemInfos(string searchPattern)
360  {
361  if (searchPattern == null)
362  {
363  throw new ArgumentNullException("searchPattern");
364  }
365  return InternalGetFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
366  }
367 
380  public FileSystemInfo[] GetFileSystemInfos(string searchPattern, SearchOption searchOption)
381  {
382  if (searchPattern == null)
383  {
384  throw new ArgumentNullException("searchPattern");
385  }
386  if (searchOption != 0 && searchOption != SearchOption.AllDirectories)
387  {
388  throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
389  }
390  return InternalGetFileSystemInfos(searchPattern, searchOption);
391  }
392 
393  private FileSystemInfo[] InternalGetFileSystemInfos(string searchPattern, SearchOption searchOption)
394  {
395  IEnumerable<FileSystemInfo> collection = FileSystemEnumerableFactory.CreateFileSystemInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
396  List<FileSystemInfo> list = new List<FileSystemInfo>(collection);
397  return list.ToArray();
398  }
399 
404  {
405  return InternalGetFileSystemInfos("*", SearchOption.TopDirectoryOnly);
406  }
407 
417  public DirectoryInfo[] GetDirectories(string searchPattern)
418  {
419  if (searchPattern == null)
420  {
421  throw new ArgumentNullException("searchPattern");
422  }
423  return InternalGetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
424  }
425 
438  public DirectoryInfo[] GetDirectories(string searchPattern, SearchOption searchOption)
439  {
440  if (searchPattern == null)
441  {
442  throw new ArgumentNullException("searchPattern");
443  }
444  if (searchOption != 0 && searchOption != SearchOption.AllDirectories)
445  {
446  throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
447  }
448  return InternalGetDirectories(searchPattern, searchOption);
449  }
450 
451  private DirectoryInfo[] InternalGetDirectories(string searchPattern, SearchOption searchOption)
452  {
453  IEnumerable<DirectoryInfo> collection = FileSystemEnumerableFactory.CreateDirectoryInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
454  List<DirectoryInfo> list = new List<DirectoryInfo>(collection);
455  return list.ToArray();
456  }
457 
463  {
464  return InternalEnumerateDirectories("*", SearchOption.TopDirectoryOnly);
465  }
466 
475  {
476  if (searchPattern == null)
477  {
478  throw new ArgumentNullException("searchPattern");
479  }
480  return InternalEnumerateDirectories(searchPattern, SearchOption.TopDirectoryOnly);
481  }
482 
493  public IEnumerable<DirectoryInfo> EnumerateDirectories(string searchPattern, SearchOption searchOption)
494  {
495  if (searchPattern == null)
496  {
497  throw new ArgumentNullException("searchPattern");
498  }
499  if (searchOption != 0 && searchOption != SearchOption.AllDirectories)
500  {
501  throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
502  }
503  return InternalEnumerateDirectories(searchPattern, searchOption);
504  }
505 
506  private IEnumerable<DirectoryInfo> InternalEnumerateDirectories(string searchPattern, SearchOption searchOption)
507  {
508  return FileSystemEnumerableFactory.CreateDirectoryInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
509  }
510 
516  {
517  return InternalEnumerateFiles("*", SearchOption.TopDirectoryOnly);
518  }
519 
527  public IEnumerable<FileInfo> EnumerateFiles(string searchPattern)
528  {
529  if (searchPattern == null)
530  {
531  throw new ArgumentNullException("searchPattern");
532  }
533  return InternalEnumerateFiles(searchPattern, SearchOption.TopDirectoryOnly);
534  }
535 
546  public IEnumerable<FileInfo> EnumerateFiles(string searchPattern, SearchOption searchOption)
547  {
548  if (searchPattern == null)
549  {
550  throw new ArgumentNullException("searchPattern");
551  }
552  if (searchOption != 0 && searchOption != SearchOption.AllDirectories)
553  {
554  throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
555  }
556  return InternalEnumerateFiles(searchPattern, searchOption);
557  }
558 
559  private IEnumerable<FileInfo> InternalEnumerateFiles(string searchPattern, SearchOption searchOption)
560  {
561  return FileSystemEnumerableFactory.CreateFileInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
562  }
563 
569  {
570  return InternalEnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly);
571  }
572 
581  {
582  if (searchPattern == null)
583  {
584  throw new ArgumentNullException("searchPattern");
585  }
586  return InternalEnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
587  }
588 
599  public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos(string searchPattern, SearchOption searchOption)
600  {
601  if (searchPattern == null)
602  {
603  throw new ArgumentNullException("searchPattern");
604  }
605  if (searchOption != 0 && searchOption != SearchOption.AllDirectories)
606  {
607  throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
608  }
609  return InternalEnumerateFileSystemInfos(searchPattern, searchOption);
610  }
611 
612  private IEnumerable<FileSystemInfo> InternalEnumerateFileSystemInfos(string searchPattern, SearchOption searchOption)
613  {
614  return FileSystemEnumerableFactory.CreateFileSystemInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
615  }
616 
627  [SecuritySafeCritical]
628  public void MoveTo(string destDirName)
629  {
630  if (destDirName == null)
631  {
632  throw new ArgumentNullException("destDirName");
633  }
634  if (destDirName.Length == 0)
635  {
636  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName");
637  }
638  Directory.CheckPermissions(base.DisplayPath, FullPath, checkHost: true, FileSecurityStateAccess.Read | FileSecurityStateAccess.Write);
639  string text = Path.GetFullPathInternal(destDirName);
640  if (!text.EndsWith(Path.DirectorySeparatorChar))
641  {
642  text += Path.DirectorySeparatorChar.ToString();
643  }
644  Directory.CheckPermissions(destDirName, text, checkHost: true, FileSecurityStateAccess.Read | FileSecurityStateAccess.Write);
645  string text2 = (!FullPath.EndsWith(Path.DirectorySeparatorChar)) ? (FullPath + Path.DirectorySeparatorChar.ToString()) : FullPath;
646  if (string.Compare(text2, text, StringComparison.OrdinalIgnoreCase) == 0)
647  {
648  throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
649  }
650  string pathRoot = Path.GetPathRoot(text2);
651  string pathRoot2 = Path.GetPathRoot(text);
652  if (string.Compare(pathRoot, pathRoot2, StringComparison.OrdinalIgnoreCase) != 0)
653  {
654  throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
655  }
656  if (!Win32Native.MoveFile(FullPath, destDirName))
657  {
658  int num = Marshal.GetLastWin32Error();
659  if (num == 2)
660  {
661  num = 3;
662  __Error.WinIOError(num, base.DisplayPath);
663  }
664  if (num == 5)
665  {
666  throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", base.DisplayPath));
667  }
668  __Error.WinIOError(num, string.Empty);
669  }
670  FullPath = text;
671  OriginalPath = destDirName;
672  base.DisplayPath = GetDisplayName(OriginalPath, FullPath);
673  _dataInitialised = -1;
674  }
675 
681  [SecuritySafeCritical]
682  public override void Delete()
683  {
684  Directory.Delete(FullPath, OriginalPath, recursive: false, checkHost: true);
685  }
686 
694  [SecuritySafeCritical]
695  public void Delete(bool recursive)
696  {
697  Directory.Delete(FullPath, OriginalPath, recursive, checkHost: true);
698  }
699 
702  public override string ToString()
703  {
704  return base.DisplayPath;
705  }
706 
707  private static string GetDisplayName(string originalPath, string fullPath)
708  {
709  string text = "";
710  if (originalPath.Length == 2 && originalPath[1] == ':')
711  {
712  return ".";
713  }
714  return originalPath;
715  }
716 
717  private static string GetDirName(string fullPath)
718  {
719  string text = null;
720  if (fullPath.Length > 3)
721  {
722  string path = fullPath;
723  if (fullPath.EndsWith(Path.DirectorySeparatorChar))
724  {
725  path = fullPath.Substring(0, fullPath.Length - 1);
726  }
727  return Path.GetFileName(path);
728  }
729  return fullPath;
730  }
731  }
732 }
FileSystemInfo [] GetFileSystemInfos(string searchPattern)
Retrieves an array of strongly typed T:System.IO.FileSystemInfo objects representing the files and su...
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
static void SetAccessControl(string path, DirectorySecurity directorySecurity)
Applies access control list (ACL) entries described by a T:System.Security.AccessControl....
Definition: Directory.cs:629
IEnumerable< DirectoryInfo > EnumerateDirectories(string searchPattern)
Returns an enumerable collection of directory information that matches a specified search pattern.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
FileInfo [] GetFiles(string searchPattern, SearchOption searchOption)
Returns a file list from the current directory matching the given search pattern and using a value to...
IEnumerable< FileInfo > EnumerateFiles(string searchPattern, SearchOption searchOption)
Returns an enumerable collection of file information that matches a specified search pattern and sear...
static DirectorySecurity GetAccessControl(string path)
Gets a T:System.Security.AccessControl.DirectorySecurity object that encapsulates the access control ...
Definition: Directory.cs:601
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
DirectorySecurity GetAccessControl(AccessControlSections includeSections)
Gets a T:System.Security.AccessControl.DirectorySecurity object that encapsulates the specified type ...
IEnumerable< FileSystemInfo > EnumerateFileSystemInfos()
Returns an enumerable collection of file system information in the current directory.
IEnumerable< FileInfo > EnumerateFiles()
Returns an enumerable collection of file information in the current directory.
void Create(DirectorySecurity directorySecurity)
Creates a directory using a T:System.Security.AccessControl.DirectorySecurity object.
IEnumerable< FileSystemInfo > EnumerateFileSystemInfos(string searchPattern)
Returns an enumerable collection of file system information that matches a specified search pattern.
override string FullName
Gets the full path of the directory.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
IEnumerable< DirectoryInfo > EnumerateDirectories(string searchPattern, SearchOption searchOption)
Returns an enumerable collection of directory information that matches a specified search pattern and...
static string GetPathRoot(string path)
Gets the root directory information of the specified path.
Definition: Path.cs:961
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
static readonly char DirectorySeparatorChar
Provides a platform-specific character used to separate directory levels in a path string that reflec...
Definition: Path.cs:17
Describes the source and destination of a given serialized stream, and provides an additional caller-...
SearchOption
Specifies whether to search the current directory, or the current directory and all subdirectories.
Definition: SearchOption.cs:8
DirectoryInfo CreateSubdirectory(string path, DirectorySecurity directorySecurity)
Creates a subdirectory or subdirectories on the specified path with the specified security....
Represents the access control and audit security for a directory. This class cannot be inherited.
IEnumerable< DirectoryInfo > EnumerateDirectories()
Returns an enumerable collection of directory information in the current directory.
void Delete(bool recursive)
Deletes this instance of a T:System.IO.DirectoryInfo, specifying whether to delete subdirectories and...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
FileSystemInfo [] GetFileSystemInfos()
Returns an array of strongly typed T:System.IO.FileSystemInfo entries representing all the files and ...
Provides properties and instance methods for the creation, copying, deletion, moving,...
Definition: FileInfo.cs:14
The file is a directory.
static void Delete(string path)
Deletes an empty directory from a specified path.
Definition: Directory.cs:1494
override string Name
Gets the name of this T:System.IO.DirectoryInfo instance.
Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition: Directory.cs:14
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
DirectoryInfo Root
Gets the root portion of the directory.
DirectoryInfo [] GetDirectories(string searchPattern, SearchOption searchOption)
Returns an array of directories in the current T:System.IO.DirectoryInfo matching the given search cr...
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
DirectoryInfo [] GetDirectories()
Returns the subdirectories of the current directory.
DirectoryInfo CreateSubdirectory(string path)
Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to...
DirectoryInfo Parent
Gets the parent directory of a specified subdirectory.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
FileInfo [] GetFiles(string searchPattern)
Returns a file list from the current directory matching the given search pattern.
The exception that is thrown when one of the arguments provided to a method is not valid.
IEnumerable< FileInfo > EnumerateFiles(string searchPattern)
Returns an enumerable collection of file information that matches a search pattern.
FileInfo [] GetFiles()
Returns a file list from the current directory.
DirectoryInfo [] GetDirectories(string searchPattern)
Returns an array of directories in the current T:System.IO.DirectoryInfo matching the given search cr...
override string ToString()
Returns the original path that was passed by the user.
DirectorySecurity GetAccessControl()
Gets a T:System.Security.AccessControl.DirectorySecurity object that encapsulates the access control ...
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
override void Delete()
Deletes this T:System.IO.DirectoryInfo if it is empty.
void Create()
Creates a directory.
FileSystemInfo [] GetFileSystemInfos(string searchPattern, SearchOption searchOption)
Retrieves an array of T:System.IO.FileSystemInfo objects that represent the files and subdirectories ...
override bool Exists
Gets a value indicating whether the directory exists.
void MoveTo(string destDirName)
Moves a T:System.IO.DirectoryInfo instance and its contents to a new path.
void Refresh()
Refreshes the state of the object.
Specifies that the class can be serialized.
DirectoryInfo(string path)
Initializes a new instance of the T:System.IO.DirectoryInfo class on the specified path.
void SetAccessControl(DirectorySecurity directorySecurity)
Applies access control list (ACL) entries described by a T:System.Security.AccessControl....
static string GetDirectoryName(string path)
Returns the directory information for the specified path string.
Definition: Path.cs:268
IEnumerable< FileSystemInfo > EnumerateFileSystemInfos(string searchPattern, SearchOption searchOption)
Returns an enumerable collection of file system information that matches a specified search pattern a...
Controls the ability to access files and folders. This class cannot be inherited.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
Provides the base class for both T:System.IO.FileInfo and T:System.IO.DirectoryInfo objects.
string FullPath
Represents the fully qualified path of the directory or file.
AccessControlSections
Specifies which sections of a security descriptor to save or load.
The P:System.Uri.LocalPath data.
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
string OriginalPath
The path originally specified by the user, whether relative or absolute.