internal static void DeleteEmptySubdirectoriesInternal(FileSystemEntryInfo fileSystemEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool initialize, PathFormat pathFormat) { #region Setup if (pathFormat == PathFormat.RelativePath) { Path.CheckValidPath(path, true, true); } if (fileSystemEntryInfo == null) { if (!File.ExistsInternal(true, transaction, path, pathFormat)) { NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path); } fileSystemEntryInfo = File.GetFileSystemEntryInfoInternal(transaction, Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck) , false, pathFormat); } if (fileSystemEntryInfo == null) { throw new ArgumentNullException("path"); } string pathLp = fileSystemEntryInfo.LongFullPath; #endregion // Setup // Ensure path is a directory. if (!fileSystemEntryInfo.IsDirectory) { throw new IOException(String.Format(CultureInfo.CurrentCulture, Resources.FileExistsWithSameNameSpecifiedByPath, pathLp)); } var dirEnumOptions = DirectoryEnumerationOptions.Folders; if (recursive) { dirEnumOptions |= DirectoryEnumerationOptions.Recursive; } foreach (var fsei in EnumerateFileSystemEntryInfosInternal <FileSystemEntryInfo>(transaction, pathLp, Path.WildcardStarMatchAll, dirEnumOptions, PathFormat.LongFullPath)) { DeleteEmptySubdirectoriesInternal(fsei, transaction, null, recursive, ignoreReadOnly, false, PathFormat.LongFullPath); } if (!EnumerateFileSystemEntryInfosInternal <string>(transaction, pathLp, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any()) { // Prevent deleting path itself. if (!initialize) { DeleteDirectoryInternal(fileSystemEntryInfo, transaction, null, false, ignoreReadOnly, true, true, PathFormat.LongFullPath); } } }
internal static IEnumerable <DriveInfo> EnumerateLogicalDrivesInternal(bool fromEnvironment, bool isReady) { #region Get from Environment if (fromEnvironment) { IEnumerable <string> drivesEnv = isReady ? Environment.GetLogicalDrives().Where(ld => File.ExistsInternal(true, null, ld, PathFormat.FullPath)) : Environment.GetLogicalDrives().Select(ld => ld); foreach (string drive in drivesEnv) { // Optionally check Drive .IsReady. if (isReady) { if (File.ExistsInternal(true, null, drive, PathFormat.FullPath)) { yield return(new DriveInfo(drive)); } } else { yield return(new DriveInfo(drive)); } } yield break; } #endregion // Get from Environment #region Get through NativeMethod uint lastError = NativeMethods.GetLogicalDrives(); if (lastError == Win32Errors.ERROR_SUCCESS) { NativeError.ThrowException((int)lastError); } uint drives = lastError; int count = 0; while (drives != 0) { if ((drives & 1) != 0) { ++count; } drives >>= 1; } string[] result = new string[count]; char[] root = { 'A', Path.VolumeSeparatorChar }; drives = lastError; count = 0; while (drives != 0) { if ((drives & 1) != 0) { string drive = new string(root); if (isReady) { // Optionally check Drive .IsReady. if (File.ExistsInternal(true, null, drive, PathFormat.FullPath)) { yield return(new DriveInfo(drive)); } } else { // Ready or not. yield return(new DriveInfo(drive)); } result[count++] = drive; } drives >>= 1; root[0]++; } #endregion // Get through NativeMethod }
public static bool Exists(KernelTransaction transaction, string path, PathFormat pathFormat) { return(File.ExistsInternal(true, transaction, path, pathFormat)); }
public static bool Exists(string path, PathFormat pathFormat) { return(File.ExistsInternal(true, null, path, pathFormat)); }
internal static void DeleteDirectoryInternal(FileSystemEntryInfo fileSystemEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool requireEmpty, bool continueOnNotExist, PathFormat pathFormat) { #region Setup if (pathFormat == PathFormat.RelativePath) Path.CheckValidPath(path, true, true); if (fileSystemEntryInfo == null) { // MSDN: .NET 3.5+: DirectoryNotFoundException: // Path does not exist or could not be found. // Path refers to a file instead of a directory. // The specified path is invalid (for example, it is on an unmapped drive). fileSystemEntryInfo = File.GetFileSystemEntryInfoInternal(transaction, Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator), continueOnNotExist, pathFormat); } if (fileSystemEntryInfo == null) return; string pathLp = fileSystemEntryInfo.LongFullPath; #endregion // Setup // Do not follow mount points nor symbolic links, but do delete the reparse point itself. // If directory is reparse point, disable recursion. if (recursive && fileSystemEntryInfo.IsReparsePoint) recursive = false; // Check to see if this is a mount point, and unmount it. if (fileSystemEntryInfo.IsMountPoint) { int lastError = Volume.DeleteVolumeMountPointInternal(pathLp, true); if (lastError != Win32Errors.ERROR_SUCCESS && lastError != Win32Errors.ERROR_PATH_NOT_FOUND) NativeError.ThrowException(lastError, pathLp); // Now it is safe to delete the actual directory. } if (recursive) { // Enumerate all file system objects. foreach (FileSystemEntryInfo fsei in EnumerateFileSystemEntryInfosInternal<FileSystemEntryInfo>(transaction, pathLp, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath)) { if (fsei.IsDirectory) DeleteDirectoryInternal(fsei, transaction, null, true, ignoreReadOnly, requireEmpty, true, PathFormat.LongFullPath); else File.DeleteFileInternal(transaction, fsei.LongFullPath, ignoreReadOnly, PathFormat.LongFullPath); } } #region Remove startRemoveDirectory: if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista // RemoveDirectory() / RemoveDirectoryTransacted() // In the ANSI version of this function, the name is limited to MAX_PATH characters. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. // 2014-09-09: MSDN confirms LongPath usage. // RemoveDirectory on a symbolic link will remove the link itself. ? NativeMethods.RemoveDirectory(pathLp) : NativeMethods.RemoveDirectoryTransacted(pathLp, transaction.SafeHandle))) { int lastError = Marshal.GetLastWin32Error(); switch ((uint)lastError) { case Win32Errors.ERROR_DIR_NOT_EMPTY: if (requireEmpty) // MSDN: .NET 3.5+: IOException: The directory specified by path is not an empty directory. throw new DirectoryNotEmptyException(pathLp); goto startRemoveDirectory; case Win32Errors.ERROR_DIRECTORY: // MSDN: .NET 3.5+: DirectoryNotFoundException: Path refers to a file instead of a directory. if (File.ExistsInternal(false, transaction, pathLp, PathFormat.LongFullPath)) throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, "({0}) {1}", Win32Errors.ERROR_INVALID_PARAMETER, String.Format(CultureInfo.CurrentCulture, Resources.FileExistsWithSameNameSpecifiedByPath, pathLp))); break; case Win32Errors.ERROR_PATH_NOT_FOUND: if (continueOnNotExist) return; break; case Win32Errors.ERROR_SHARING_VIOLATION: // MSDN: .NET 3.5+: IOException: The directory is being used by another process or there is an open handle on the directory. NativeError.ThrowException(lastError, pathLp); break; case Win32Errors.ERROR_ACCESS_DENIED: NativeMethods.Win32FileAttributeData data = new NativeMethods.Win32FileAttributeData(); int dataInitialised = File.FillAttributeInfoInternal(transaction, pathLp, ref data, false, true); if (data.FileAttributes != (FileAttributes)(-1)) { if ((data.FileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { // MSDN: .NET 3.5+: IOException: The directory specified by path is read-only. if (ignoreReadOnly) { // Reset directory attributes. File.SetAttributesInternal(true, transaction, pathLp, FileAttributes.Normal, true, PathFormat.LongFullPath); goto startRemoveDirectory; } // MSDN: .NET 3.5+: IOException: The directory is read-only. throw new DirectoryReadOnlyException(pathLp); } } if (dataInitialised == Win32Errors.ERROR_SUCCESS) // MSDN: .NET 3.5+: UnauthorizedAccessException: The caller does not have the required permission. NativeError.ThrowException(lastError, pathLp); break; } // MSDN: .NET 3.5+: IOException: // A file with the same name and location specified by path exists. // The directory specified by path is read-only, or recursive is false and path is not an empty directory. // The directory is the application's current working directory. // The directory contains a read-only file. // The directory is being used by another process. NativeError.ThrowException(lastError, pathLp); } #endregion // Remove }