SHFileOperation - Problem with file paths containing accents

Steeve 1 Reputation point
2022-04-21T09:29:58.037+00:00

Hello,

I have a problem with the SHFileOperation function of the shell32.dll. Files containing accents are not recognized and returns error #2 "File_Not_Found".
Thanks for your help.

Here is the code. If the function returns error #2, I have to do it with io.file.copy

        Private Declare Function SHFileOperation Lib "shell32.dll" _
            Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer


        Public Shared Sub CopyFile(ByVal owner As IWin32Window, ByVal source As String, ByVal Destination As String, Optional ByVal Index As Integer = 0)

            Dim lFileOp As Short
            Dim lresult As Short
            Dim lFlags As Short
            Dim SHFileOp As SHFILEOPSTRUCT

            Select Case Index
                Case 0
                    lFileOp = FO_Func.FO_COPY
                Case 1
                    lFileOp = FO_Func.FO_MOVE
                Case 2
                    lFileOp = FO_Func.FO_RENAME
                Case 3
                    lFileOp = FO_Func.FO_DELETE
            End Select

            lFlags = lFlags Or FO_Func.FOF_FILESONLY Or FO_Func.FOF_SIMPLEPROGRESS  

            Try
                With SHFileOp
                    .hwnd = owner.Handle.ToInt32 
                    .wFunc = lFileOp
                    .pFrom = source & vbNullChar & vbNullChar
                    .pTo = Destination & vbNullChar & vbNullChar
                    .fFlags = FO_Func.FOF_ALLOWUNDO
                    .lpszProgressTitle = "Copy file"
                End With
                lresult = SHFileOperation(SHFileOp)

                If lresult <> 0 Or SHFileOp.fAnyOperationsAborted Then
                    Dim Mes As String = ""

                    If lresult = 2 And Io.File.Exists(source) Then
                        IO.File.Copy(source, Destination)                       ' Forced to do this with io.file.copy'
                    Else
                        InitErreurs()
                        If Errors.ContainsKey(lresult) Then
                            Mes = Errors.Values(lresult)
                        End If
                        MessagePopupShow(FormPrincipal, 0, ETypeMessagePopup.Avorter, "Erreur", Mes)
                    End If
                    Exit Sub

                Else
                    MsgBox("Operation Complete", vbInformation, "File Operations")
                End If

                   Catch ex As Exception

            End Try

        End Sub

Excuse me for my English

Steeve

Developer technologies VB
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-04-21T10:02:24.523+00:00

    It works for me with Unicode :

        <DllImport("Shell32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, EntryPoint:="SHFileOperationW")>
        Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
        End Function
    
    0 comments No comments

  2. Steeve 1 Reputation point
    2022-04-21T15:59:28.273+00:00

    Thanks but I changed the W to A EntryPoint:="SHFileOperationA"
    With W it didn't always work !

    <DllImport("Shell32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, EntryPoint:="SHFileOperationA")>
         Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
         End Function
    
    0 comments No comments

  3. Steeve 1 Reputation point
    2022-05-25T08:28:09.677+00:00

    @Castorix31 Hello, I open the Post again because the problem does not seem to be completely solved !

    Example of problem file :

    Before lresult = SHFileOperation(SHFileOp)

    ? shfileop.pFrom
    "C:\Users\Deschryvers\OneDrive\Deuse\Deuse_devis_20220329_application web_génération bon de commande.pdf"
    ? shfileop.pTo
    "C:\Users\Deschryvers\OneDrive\Deuse\OLD\Deuse_devis_20220329_application web_génération bon de commande.pdf"

    After lesult = SHFileOperation(SHFileOp)

    lresult = 2 ' = FILE_NOT_FOUND because
    ? shfileop.pFrom
    "C:\Users\Deschryvers\OneDrive\Deuse\Deuse_devis_20220329_application web_ge´ne´ration bon de commande.pdf"
    ? shfileop.pTo
    "C:\Users\Deschryvers\OneDrive\Deuse\OLD\Deuse_devis_20220329_application web_ge´ne´ration bon de commande.pdf"

    Transformation of accents é => e´ ! : So it is logical that it does not find the file but why?

    Thanks for your help

    Steeve

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.