예제 #1
0
        /// <summary>
        /// Gets a <see cref="DismException"/> or <see cref="Exception"/> for the specified error code.
        /// </summary>
        /// <param name="errorCode">The error code to get an exception for.</param>
        /// <returns>A <see cref="DismException"/> or <see cref="Exception"/> that represents the error code.</returns>
        internal static Exception GetDismExceptionForHResult(int errorCode)
        {
            // Look for known error codes
            switch ((uint)errorCode)
            {
            case Win32Error.ERROR_REQUEST_ABORTED:
            case 0x80070000 | Win32Error.ERROR_REQUEST_ABORTED:
                return(new OperationCanceledException());

            case Win32Error.ERROR_SUCCESS_REBOOT_REQUIRED:
                return(new DismRebootRequiredException(errorCode));

            case DismApi.DISMAPI_E_DISMAPI_NOT_INITIALIZED:
                // User has not called DismApi.Initialize()
                return(new DismNotInitializedException(errorCode));

            case DismApi.DISMAPI_E_OPEN_SESSION_HANDLES:
                // User has not called CloseSession() on open sessions
                return(new DismOpenSessionsException(errorCode));
            }

            // Attempt to get an error message from the DismApi
            string lastError = DismApi.GetLastErrorMessage();

            // See if the result is not null
            if (!String.IsNullOrEmpty(lastError))
            {
                // Return a DismException object
                return(new DismException(errorCode, lastError.Trim()));
            }

            // Return an Exception for the HResult
            return(Marshal.GetExceptionForHR(errorCode));
        }