private void Cancel()
        {
            // Storing to locals to avoid data ----s
            SafeHandle        handle     = this._handle;
            NativeOverlapped *overlapped = this._overlapped;

            if (handle != null && !handle.IsInvalid && overlapped != null)
            {
                if (!UnsafeNativeMethods.CancelIoEx(handle, overlapped))
                {
                    // This case should not have any consequences although
                    // it will be easier to debug if there exists any special case
                    // we are not aware of.
                    int errorCode = Marshal.GetLastWin32Error();
                    Debug.WriteLine("CancelIoEx finished with error code {0}.", errorCode);
                }
                SetOperationCompleted();
            }
        }
Beispiel #2
0
        internal static void WinIOError(int errorCode, String maybeFullPath)
        {
            // This doesn't have to be perfect, but is a perf optimization.
            bool   isInvalidPath = errorCode == UnsafeNativeMethods.ERROR_INVALID_NAME || errorCode == UnsafeNativeMethods.ERROR_BAD_PATHNAME;
            String str           = GetDisplayablePath(maybeFullPath, isInvalidPath);

            switch (errorCode)
            {
            case UnsafeNativeMethods.ERROR_FILE_NOT_FOUND:
                if (str.Length == 0)
                {
                    throw new FileNotFoundException(SR.GetString(SR.IO_FileNotFound));
                }
                else
                {
                    throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.IO_FileNotFound_FileName), str), str);
                }

            case UnsafeNativeMethods.ERROR_PATH_NOT_FOUND:
                if (str.Length == 0)
                {
                    throw new DirectoryNotFoundException(SR.GetString(SR.IO_PathNotFound_NoPathName));
                }
                else
                {
                    throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.IO_PathNotFound_Path), str));
                }

            case UnsafeNativeMethods.ERROR_ACCESS_DENIED:
                if (str.Length == 0)
                {
                    throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_NoPathName));
                }
                else
                {
                    throw new UnauthorizedAccessException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.UnauthorizedAccess_IODenied_Path), str));
                }

            case UnsafeNativeMethods.ERROR_ALREADY_EXISTS:
                if (str.Length == 0)
                {
                    goto default;
                }
                throw new IOException(SR.GetString(SR.IO_IO_AlreadyExists_Name, str), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));

            case UnsafeNativeMethods.ERROR_FILENAME_EXCED_RANGE:
                throw new PathTooLongException(SR.GetString(SR.IO_PathTooLong));

            case UnsafeNativeMethods.ERROR_INVALID_DRIVE:
                throw new DriveNotFoundException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.IO_DriveNotFound_Drive), str));

            case UnsafeNativeMethods.ERROR_INVALID_PARAMETER:
                throw new IOException(UnsafeNativeMethods.GetMessage(errorCode), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));

            case UnsafeNativeMethods.ERROR_SHARING_VIOLATION:
                if (str.Length == 0)
                {
                    throw new IOException(SR.GetString(SR.IO_IO_SharingViolation_NoFileName), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));
                }
                else
                {
                    throw new IOException(SR.GetString(SR.IO_IO_SharingViolation_File, str), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));
                }

            case UnsafeNativeMethods.ERROR_FILE_EXISTS:
                if (str.Length == 0)
                {
                    goto default;
                }
                throw new IOException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.IO_IO_FileExists_Name), str), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));

            case UnsafeNativeMethods.ERROR_OPERATION_ABORTED:
                throw new OperationCanceledException();

            default:
                throw new IOException(UnsafeNativeMethods.GetMessage(errorCode), UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));
            }
        }