public static Exception GetLastWin32ErrorException(IAbsoluteDirectoryPath.Impl path) { int error = Marshal.GetLastWin32Error(); Debug.Assert(error != 0, "no error"); var win32Ex = new Win32Exception(error); string message = $"{win32Ex.Message} Path: '{path.PathDisplay}'."; switch (error) { case WindowsNative.Errors.FILE_NOT_FOUND: case WindowsNative.Errors.PATH_NOT_FOUND: case WindowsNative.Errors.INVALID_DRIVE: return(new DirectoryNotFoundException(message, win32Ex)); case WindowsNative.Errors.ACCESS_DENIED: return(new UnauthorizedIOAccessException(message, win32Ex)); case WindowsNative.Errors.FILENAME_EXCED_RANGE: return(new PathTooLongException(message, win32Ex)); default: if (path.PathFormat == PathFormat.Windows) { path.EnsureExists(); // Throw DirectoryNotFound exception instead of IOException if path is a file. } return(new IOException(message, win32Ex)); } }
public static void GetSpace(IAbsoluteDirectoryPath.Impl path, out long availableBytes, out long totalBytes, out long freeBytes) { using (MediaInsertionPromptGuard.Enter()) { if (!WindowsNative.GetDiskFreeSpaceEx(path.PathExport, out availableBytes, out totalBytes, out freeBytes)) { throw GetLastWin32ErrorException(path); } } }
public static unsafe string GetFileSystem(IAbsoluteDirectoryPath.Impl rootDir) { // rootDir must be a symlink or root drive const int MAX_LENGTH = 261; // MAX_PATH + 1 char *fileSystemName = stackalloc char[MAX_LENGTH]; using (MediaInsertionPromptGuard.Enter()) { if (!WindowsNative.GetVolumeInformation(rootDir.PathExportWithTrailingSeparator, null, 0, null, null, out int fileSystemFlags, fileSystemName, MAX_LENGTH)) { throw GetLastWin32ErrorException(rootDir); } } return(new string(fileSystemName)); }
public static DriveType GetDriveType(IAbsoluteDirectoryPath.Impl path) => WindowsNative.GetDriveType(path.PathExportWithTrailingSeparator);