Esempio n. 1
0
        public static VolumeInfo GetVolumeInformation(FileStream file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            StringBuilder volumeNameBuffer       = new StringBuilder(NativeMethods.MAX_PATH + 1);
            StringBuilder fileSystemNameBuffer   = new StringBuilder(NativeMethods.MAX_PATH + 1);
            uint          serialNumber           = 0;
            uint          maximumComponentLength = 0;
            uint          fileSystemFlags        = 0;

            if (!NativeMethods.GetVolumeInformationByHandleW(file.SafeFileHandle, volumeNameBuffer, volumeNameBuffer.Capacity,
                                                             out serialNumber, out maximumComponentLength, out fileSystemFlags, fileSystemNameBuffer,
                                                             fileSystemNameBuffer.Capacity))
            {
                int       error = Marshal.GetLastWin32Error();
                Exception e     = Marshal.GetExceptionForHR(Win32Errors.GetHRFromWin32Error((uint)error));
                if (error == Win32Errors.ERROR_NOT_READY)
                {
                    throw new DeviceNotReadyException("Device not ready", e);
                }

                throw e;
            }

            return(new VolumeInfo(volumeNameBuffer.ToString(), fileSystemFlags, serialNumber,
                                  (int)maximumComponentLength, fileSystemNameBuffer.ToString()));
        }
Esempio n. 2
0
        public static void ThrowException(uint errorCode, string readPath, string writePath)
        {
            string errorMessage = string.Format(CultureInfo.CurrentCulture, "({0}) {1}.", errorCode, new Win32Exception((int)errorCode).Message);

            if (!Utils.IsNullOrWhiteSpace(readPath))
            {
                errorMessage = string.Format(CultureInfo.CurrentCulture, "{0}: [{1}]", errorMessage.TrimEnd('.'), readPath);
            }

            if (!Utils.IsNullOrWhiteSpace(writePath))
            {
                errorMessage = string.Format(CultureInfo.CurrentCulture, "{0}: [{1}]", errorMessage.TrimEnd('.'), writePath);
            }

            switch (errorCode)
            {
            case Win32Errors.ERROR_INVALID_DRIVE:
                throw new DriveNotFoundException(errorMessage);

            case Win32Errors.ERROR_OPERATION_ABORTED:
                throw new OperationCanceledException(errorMessage);

            case Win32Errors.ERROR_FILE_NOT_FOUND:
                throw new FileNotFoundException(errorMessage);

            case Win32Errors.ERROR_PATH_NOT_FOUND:
                throw new DirectoryNotFoundException(errorMessage);

            case Win32Errors.ERROR_BAD_RECOVERY_POLICY:
                throw new PolicyException(errorMessage);

            case Win32Errors.ERROR_FILE_READ_ONLY:
            case Win32Errors.ERROR_ACCESS_DENIED:
            case Win32Errors.ERROR_NETWORK_ACCESS_DENIED:
                throw new UnauthorizedAccessException(errorMessage);

            case Win32Errors.ERROR_ALREADY_EXISTS:
            case Win32Errors.ERROR_FILE_EXISTS:
                throw new AlreadyExistsException(errorMessage);

            case Win32Errors.ERROR_DIR_NOT_EMPTY:
                throw new DirectoryNotEmptyException(errorMessage);

            case Win32Errors.ERROR_NOT_READY:
                throw new DeviceNotReadyException(errorMessage);


                #region Transactional

            case Win32Errors.ERROR_INVALID_TRANSACTION:
                throw new InvalidTransactionException(Resources.Transaction_Invalid, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTION_ALREADY_COMMITTED:
                throw new TransactionAlreadyCommittedException(Resources.Transaction_Already_Committed, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTION_ALREADY_ABORTED:
                throw new TransactionAlreadyAbortedException(Resources.Transaction_Already_Aborted, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTIONAL_CONFLICT:
                throw new TransactionalConflictException(Resources.Transactional_Conflict, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTION_NOT_ACTIVE:
                throw new TransactionException(Resources.Transaction_Not_Active, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTION_NOT_REQUESTED:
                throw new TransactionException(Resources.Transaction_Not_Requested, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTION_REQUEST_NOT_VALID:
                throw new TransactionException(Resources.Invalid_Transaction_Request, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_TRANSACTIONS_UNSUPPORTED_REMOTE:
                throw new UnsupportedRemoteTransactionException(Resources.Invalid_Transaction_Request, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

            case Win32Errors.ERROR_NOT_A_REPARSE_POINT:
                throw new NotAReparsePointException(Resources.Not_A_Reparse_Point, Marshal.GetExceptionForHR(Win32Errors.GetHrFromWin32Error(errorCode)));

                #endregion // Transacted

            case Win32Errors.ERROR_SUCCESS:
            case Win32Errors.ERROR_SUCCESS_REBOOT_INITIATED:
            case Win32Errors.ERROR_SUCCESS_REBOOT_REQUIRED:
            case Win32Errors.ERROR_SUCCESS_RESTART_REQUIRED:
                // We should really never get here, throwing an exception for a successful operation.
                throw new NotImplementedException(string.Format(CultureInfo.CurrentCulture, "{0} {1}", Resources.Exception_From_Successful_Operation, errorMessage));

            default:
                // We don't have a specific exception to generate for this error.
                throw new IOException(errorMessage, Win32Errors.GetHrFromWin32Error(errorCode));
            }
        }
Esempio n. 3
0
        public static void ThrowException(uint errorCode, string readPath, string writePath)
        {
            // Generate an exception representing the error from Marshal. We embed this as an
            // inner exception if we decide to generate our own exception.
            Exception e = Marshal.GetExceptionForHR(Win32Errors.GetHRFromWin32Error(errorCode));

            switch (errorCode)
            {
            case Win32Errors.ERROR_FILE_NOT_FOUND:
                throw new System.IO.FileNotFoundException("File not found", readPath ?? (writePath ?? String.Empty), e);

            case Win32Errors.ERROR_INVALID_TRANSACTION:
                throw new InvalidTransactionException(Resources.InvalidTransaction, e);

            case Win32Errors.ERROR_ALREADY_EXISTS:
            case Win32Errors.ERROR_FILE_EXISTS:
                throw new AlreadyExistsException(Resources.PathAlreadyExists, writePath ?? String.Empty, e);

            case Win32Errors.ERROR_PATH_NOT_FOUND:
                throw new System.IO.DirectoryNotFoundException(FormatError(Resources.DirectoryNotFound, readPath ?? writePath), e);

            case Win32Errors.ERROR_DIRECTORY:
                throw new NotSupportedException(FormatError(Resources.InvalidDirectoryName, readPath ?? writePath), e);

            case Win32Errors.ERROR_DIR_NOT_EMPTY:
                throw new DirectoryNotEmptyException(FormatError(Resources.DirectoryNotEmpty, writePath), e);

            case Win32Errors.ERROR_TRANSACTION_ALREADY_COMMITTED:
                throw new TransactionAlreadyCommittedException(Resources.TransactionAlreadyCommitted, e);

            case Win32Errors.ERROR_TRANSACTION_ALREADY_ABORTED:
                throw new TransactionAlreadyAbortedException(Resources.TransactionAlreadyAborted, e);

            case Win32Errors.ERROR_TRANSACTIONAL_CONFLICT:
                throw new TransactionalConflictException(Resources.TransactionalConflict, e);

            case Win32Errors.ERROR_TRANSACTION_NOT_ACTIVE:
                throw new TransactionException(Resources.TransactionNotActive, e);

            case Win32Errors.ERROR_TRANSACTION_NOT_REQUESTED:
                throw new TransactionException(Resources.TransactionNotRequested, e);

            case Win32Errors.ERROR_TRANSACTION_REQUEST_NOT_VALID:
                throw new TransactionException(Resources.InvalidTransactionRequest, e);

            case Win32Errors.ERROR_TRANSACTIONS_UNSUPPORTED_REMOTE:
                throw new UnsupportedRemoteTransactionException(Resources.InvalidTransactionRequest, e);

            case Win32Errors.ERROR_NOT_A_REPARSE_POINT:
                throw new NotAReparsePointException(Resources.NotAReparsePoint, e);

            case Win32Errors.ERROR_SUCCESS:
            case Win32Errors.ERROR_SUCCESS_REBOOT_INITIATED:
            case Win32Errors.ERROR_SUCCESS_REBOOT_REQUIRED:
            case Win32Errors.ERROR_SUCCESS_RESTART_REQUIRED:
                // We should really never get here, throwing an exception for a successful operation.
                throw new NotImplementedException(
                          String.Format(CultureInfo.CurrentCulture, Resources.AlphaFSInternalError +
                                        Resources.AttemptingToGenerateExceptionFromSuccessfulOperation +
                                        Resources.ErrorCodeWas0, errorCode));

            case Win32Errors.ERROR_ACCESS_DENIED:
                throw new UnauthorizedAccessException("Access denied.", e);

            default:
                // We don't have a specific exception to generate for this error
                throw e;
            }
        }