/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Security; namespace Alphaleonis.Win32.Filesystem { /// Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited. [SerializableAttribute] public sealed partial class DirectoryInfo : FileSystemInfo { #region Constructors #region .NET /// Initializes a new instance of the class on the specified path. /// The path on which to create the . /// /// This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations. /// The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share. /// public DirectoryInfo(string path) : this(null, path, PathFormat.RelativePath) { } #endregion // .NET #region AlphaFS /// [AlphaFS] Initializes a new instance of the class on the specified path. /// The path on which to create the . /// Indicates the format of the path parameter(s). /// This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations. public DirectoryInfo(string path, PathFormat pathFormat) : this(null, path, pathFormat) { } /// [AlphaFS] Special internal implementation. /// The transaction. /// The full path on which to create the . /// Not used. /// Not used. /// This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "junk1")] [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "junk2")] private DirectoryInfo(KernelTransaction transaction, string fullPath, bool junk1, bool junk2) { IsDirectory = true; Transaction = transaction; LongFullName = Path.GetLongPathCore(fullPath, GetFullPathOptions.None); OriginalPath = Path.GetFileName(fullPath, true); FullPath = fullPath; DisplayPath = OriginalPath.Length != 2 || OriginalPath[1] != Path.VolumeSeparatorChar ? OriginalPath : Path.CurrentDirectoryPrefix; } #region Transactional /// [AlphaFS] Initializes a new instance of the class on the specified path. /// The transaction. /// The path on which to create the . /// This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations. public DirectoryInfo(KernelTransaction transaction, string path) : this(transaction, path, PathFormat.RelativePath) { } /// [AlphaFS] Initializes a new instance of the class on the specified path. /// The transaction. /// The path on which to create the . /// Indicates the format of the path parameter(s). /// This constructor does not check if a directory exists. This constructor is a placeholder for a string that is used to access the disk in subsequent operations. public DirectoryInfo(KernelTransaction transaction, string path, PathFormat pathFormat) { InitializeCore(true, transaction, path, pathFormat); } #endregion // Transactional #endregion // AlphaFS #endregion // Constructors #region Properties #region .NET #region Exists /// Gets a value indicating whether the directory exists. /// /// The property returns if any error occurs while trying to determine if the /// specified directory exists. /// This can occur in situations that raise exceptions such as passing a directory name with invalid characters or too many /// characters, /// a failing or missing disk, or if the caller does not have permission to read the directory. /// /// if the directory exists; otherwise, . [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] public override bool Exists { [SecurityCritical] get { try { if (DataInitialised == -1) Refresh(); FileAttributes attrs = Win32AttributeData.dwFileAttributes; return DataInitialised == 0 && attrs != (FileAttributes) (-1) && (attrs & FileAttributes.Directory) != 0; } catch { return false; } } } #endregion // Exists #region Name /// Gets the name of this instance. /// The directory name. /// /// This Name property returns only the name of the directory, such as "Bin". /// To get the full path, such as "c:\public\Bin", use the FullName property. /// public override string Name { get { // GetDirName() return FullPath.Length > 3 ? Path.GetFileName(Path.RemoveTrailingDirectorySeparator(FullPath, false), true) : FullPath; } } #endregion // Name #region Parent /// Gets the parent directory of a specified subdirectory. /// The parent directory, or null if the path is null or if the file path denotes a root (such as "\", "C:", or * "\\server\share"). public DirectoryInfo Parent { [SecurityCritical] get { string path = FullPath; if (path.Length > 3) path = Path.RemoveTrailingDirectorySeparator(FullPath, false); string dirName = Path.GetDirectoryName(path, false); return dirName == null ? null : new DirectoryInfo(Transaction, dirName, true, true); } } #endregion // Parent #region Root /// Gets the root portion of the directory. /// An object that represents the root of the directory. public DirectoryInfo Root { [SecurityCritical] get { return new DirectoryInfo(Transaction, Path.GetPathRoot(FullPath, false), PathFormat.RelativePath); } } #endregion // Root #endregion // .NET #endregion // Properties } }