Skip to content

Commit

Permalink
Convert SHFileOperation error to Win IO error (#74607)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehasis authored Aug 28, 2022
1 parent acfd683 commit 48a3dbe
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ Namespace Microsoft.VisualBasic.FileIO
Throw New OperationCanceledException()
End If
ElseIf Result <> 0 Then
ThrowWinIOError(Result)
ThrowWinIOError(ToWinIOErrorCode(Result))
End If
End Sub

Expand Down Expand Up @@ -1905,6 +1905,70 @@ Namespace Microsoft.VisualBasic.FileIO
End Select
End Function

#If TARGET_WINDOWS Then
''' <summary>
''' Convert SHFileOperation error code to Win IO error code.
''' </summary>
''' <param name="errorCode">SHFileOperation error code.</param>
''' <returns>Win IO error code.</returns>
Private Shared Function ToWinIOErrorCode(ByVal errorCode As Integer) As Integer
Select Case errorCode
Case NativeTypes.DE_SAMEFILE
Return NativeTypes.ERROR_ALREADY_EXISTS
Case NativeTypes.DE_MANYSRC1DEST
Return NativeTypes.ERROR_INVALID_PARAMETER
Case NativeTypes.DE_DIFFDIR
Return NativeTypes.ERROR_NOT_SAME_DEVICE
Case NativeTypes.DE_ROOTDIR
Return NativeTypes.ERROR_ACCESS_DENIED
Case NativeTypes.DE_OPCANCELLED
Return NativeTypes.ERROR_CANCELLED
Case NativeTypes.DE_DESTSUBTREE
Return NativeTypes.ERROR_BAD_PATHNAME
Case NativeTypes.DE_ACCESSDENIEDSRC
Return NativeTypes.ERROR_ACCESS_DENIED
Case NativeTypes.DE_PATHTOODEEP
Return NativeTypes.ERROR_FILENAME_EXCED_RANGE
Case NativeTypes.DE_MANYDEST
Return NativeTypes.ERROR_INVALID_PARAMETER
Case NativeTypes.DE_INVALIDFILES
Return NativeTypes.ERROR_BAD_PATHNAME
Case NativeTypes.DE_DESTSAMETREE
Return NativeTypes.ERROR_INVALID_PARAMETER
Case NativeTypes.DE_FLDDESTISFILE
Return NativeTypes.ERROR_ALREADY_EXISTS
Case NativeTypes.DE_FILEDESTISFLD
Return NativeTypes.ERROR_ALREADY_EXISTS
Case NativeTypes.DE_FILENAMETOOLONG
Return NativeTypes.ERROR_FILENAME_EXCED_RANGE
Case NativeTypes.DE_DEST_IS_CDROM
Return NativeTypes.ERROR_WRITE_FAULT
Case NativeTypes.DE_DEST_IS_DVD
Return NativeTypes.ERROR_WRITE_FAULT
Case NativeTypes.DE_DEST_IS_CDRECORD
Return NativeTypes.ERROR_WRITE_FAULT
Case NativeTypes.DE_FILE_TOO_LARGE
Return NativeTypes.ERROR_FILE_TOO_LARGE
Case NativeTypes.DE_SRC_IS_CDROM
Return NativeTypes.ERROR_READ_FAULT
Case NativeTypes.DE_SRC_IS_DVD
Return NativeTypes.ERROR_READ_FAULT
Case NativeTypes.DE_SRC_IS_CDRECORD
Return NativeTypes.ERROR_READ_FAULT
Case NativeTypes.DE_ERROR_MAX
Return NativeTypes.ERROR_FILENAME_EXCED_RANGE
Case NativeTypes.DE_ERROR_UNKNOWN
Return NativeTypes.ERROR_PATH_NOT_FOUND
Case NativeTypes.ERRORONDEST
Return NativeTypes.ERROR_GEN_FAILURE
Case NativeTypes.DE_ROOTDIR_ERRORONDEST
Return NativeTypes.ERROR_ACCESS_DENIED
Case Else
Return errorCode
End Select
End Function
#End If

''' <summary>
''' Verify that the given argument value is a valid DeleteDirectoryOption. If not, throw InvalidEnumArgumentException.
''' </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ Namespace Microsoft.VisualBasic.CompilerServices
Friend Const ERROR_FILE_EXISTS As Integer = 80
Friend Const ERROR_OPERATION_ABORTED As Integer = 995
Friend Const ERROR_CANCELLED As Integer = 1223
Friend Const ERROR_NOT_SAME_DEVICE As Integer = 17
Friend Const ERROR_WRITE_FAULT As Integer = 29
Friend Const ERROR_READ_FAULT As Integer = 30
Friend Const ERROR_GEN_FAILURE As Integer = 31
Friend Const ERROR_BAD_PATHNAME As Integer = 161
Friend Const ERROR_FILE_TOO_LARGE As Integer = 223

' Error code from SHFileOperationW function (shellapi.h)
Friend Const DE_SAMEFILE As Integer = 113
Friend Const DE_MANYSRC1DEST As Integer = 114
Friend Const DE_DIFFDIR As Integer = 115
Friend Const DE_ROOTDIR As Integer = 116
Friend Const DE_OPCANCELLED As Integer = 117
Friend Const DE_DESTSUBTREE As Integer = 118
Friend Const DE_ACCESSDENIEDSRC As Integer = 120
Friend Const DE_PATHTOODEEP As Integer = 121
Friend Const DE_MANYDEST As Integer = 122
Friend Const DE_INVALIDFILES As Integer = 124
Friend Const DE_DESTSAMETREE As Integer = 125
Friend Const DE_FLDDESTISFILE As Integer = 126
Friend Const DE_FILEDESTISFLD As Integer = 128
Friend Const DE_FILENAMETOOLONG As Integer = 129
Friend Const DE_DEST_IS_CDROM As Integer = 130
Friend Const DE_DEST_IS_DVD As Integer = 131
Friend Const DE_DEST_IS_CDRECORD As Integer = 132
Friend Const DE_FILE_TOO_LARGE As Integer = 133
Friend Const DE_SRC_IS_CDROM As Integer = 134
Friend Const DE_SRC_IS_DVD As Integer = 135
Friend Const DE_SRC_IS_CDRECORD As Integer = 136
Friend Const DE_ERROR_MAX As Integer = 183
Friend Const DE_ERROR_UNKNOWN As Integer = 1026
Friend Const ERRORONDEST As Integer = 65536
Friend Const DE_ROOTDIR_ERRORONDEST As Integer = 65652

''' ;New
''' <summary>
Expand Down

0 comments on commit 48a3dbe

Please sign in to comment.