开发者

Change pinvoke SHFileOperation root directory

开发者 https://www.devze.com 2023-03-27 18:42 出处:网络
This is probably a question that nobody even know what I am talking about... But here it goes. So, I want to delete, copy, move, and rename files using the actual Windows confirmation dialogs.

This is probably a question that nobody even know what I am talking about... But here it goes.

So, I want to delete, copy, move, and rename files using the actual Windows confirmation dialogs. I am using SHFileOperation. My problem is, that, when I want to move a folder with this path:

"C:\MyFolderToMove"

And I set the destination to:

"C:\Users\Test"

And (THIS PART IS IMPORTANT) the APPLICATION start from say

"D:\MyApp.exe"

SHFileOperation will try to move it to:

"D:\C\Users\Test"

So, it basically combines the "start-in" folder of the application with th开发者_JAVA技巧e destination you specify.

Does ANYONE have an idea of how to resolve this?

Here's the code, just in case someone wants to see it. (It's in VB.NET, but I also understand C#.NET.)

Imports System.Runtime.InteropServices

Namespace SHFileOperation

    Public Module SHFileOperation

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

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
        Private Structure SHFILEOPSTRUCT
            Public hwnd As IntPtr
            Public wFunc As Operation
            <MarshalAs(UnmanagedType.LPWStr)> _
            Public pFrom As String
            <MarshalAs(UnmanagedType.LPWStr)> _
            Public pTo As String
            Public fFlags As FileOperationFlags
            Public fAnyOperationsAborted As Boolean
            Public hNameMappings As IntPtr
            <MarshalAs(UnmanagedType.LPWStr)> _
            Public lpszProgressTitle As String '  only used if FOF_SIMPLEPROGRESS
        End Structure

        <Flags()> Public Enum FileOperationFlags
            FOF_MULTIDESTFILES = &H1
            FOF_CONFIRMMOUSE = &H2
            FOF_SILENT = &H4
            FOF_RENAMEONCOLLISION = &H8
            FOF_NOCONFIRMATION = &H10
            FOF_WANTMAPPINGHANDLE = &H20
            FOF_ALLOWUNDO = &H40
            FOF_FILESONLY = &H80
            FOF_SIMPLEPROGRESS = &H100
            FOF_NOCONFIRMMKDIR = &H200
            FOF_NOERRORUI = &H400
            FOF_NOCOPYSECURITYATTRIBS = &H800
            FOF_NORECURSION = &H1000
            FOF_NO_CONNECTED_ELEMENTS = &H2000
            FOF_WANTNUKEWARNING = &H4000
            FOF_NORECURSEREPARSE = &H8000
        End Enum

        Public Enum Operation As UInteger
            Move = &H1
            Copy = &H2
            Delete = &H3
            Rename = &H4
        End Enum

        Public Sub MoveFiles(ByVal File As String(), ByVal DestinationDirectory As String)
            Dim Struct As New SHFILEOPSTRUCT With {.hwnd = Nothing,
                                                   .wFunc = Operation.Move,
                                                   .pTo = DestinationDirectory & "\test",
                                                   .fFlags = FileOperationFlags.FOF_ALLOWUNDO Or FileOperationFlags.FOF_WANTNUKEWARNING}

            Dim Files As New Text.StringBuilder()
            For Each F As String In File
                Files.AppendFormat("{0}" & vbNullChar, F)
            Next
            Struct.pFrom = Files.ToString

            SHFileOperation(Struct)
        End Sub
        Public Sub MoveFiles(ByVal File As String, ByVal DestinationDirectory As String)
            MoveFiles(New String() {File}, DestinationDirectory)
        End Sub

    End Module

End Namespace


Your destination directory isn't double-null terminated (see the documentation for SHFILEOPSTRUCT). Change the code to:

.pTo = DestinationDirectory & "\test" & vbNullChar,
0

精彩评论

暂无评论...
验证码 换一张
取 消