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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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
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
@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