更改 pinvoke SHFileOperation 根目录

发布于 2024-11-29 08:30:22 字数 3302 浏览 1 评论 0原文

这可能是一个没人知道我在说什么的问题……但事情就是这样。

因此,我想使用实际的 Windows 确认对话框来删除、复制、移动和重命名文件。 我正在使用 SHFileOperation。我的问题是,当我想移动具有以下路径的文件夹时:
“C:\MyFolderToMove”
我将目的地设置为:
“C:\用户\测试”
(这部分很重要)应用程序从说
开始 “D:\MyApp.exe”
SHFileOperation 将尝试将其移动到:
“D:\C\Users\Test”

因此,它基本上将应用程序的“起始”文件夹与您指定的目标结合在一起。

有人知道如何解决这个问题吗?
这是代码,以防有人想看。 (它是用 VB.NET 编写的,但我也了解 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

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 the 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-12-06 08:30:22

您的目标目录不是双空终止的(请参阅 SHFILEOPSTRUCT)。将代码更改为:

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

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

.pTo = DestinationDirectory & "\test" & vbNullChar,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文