更改pinvoke SHFileOperation根目录

时间:2011-08-13 22:25:04

标签: vb.net pinvoke

这可能是一个人们甚至不知道我在说什么的问题......但是现在就这样了。

因此,我想使用实际的Windows确认对话框删除,复制,移动和重命名文件。 我正在使用SHFileOperation。我的问题是,当我想用​​这条路径移动文件夹时:
“C:\ MyFolderToMove”
我将目的地设置为:
“C:\用户\测试”
并且(这部分是重要的) APPLICATION 从说出来开始 “d:\ MyApp.exe的”
SHFileOperation将尝试将其移至:
“d:\ C \用户\测试”

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

是否有人知道如何解决这个问题? 这是代码,以防万一有人想看到它。 (它在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

1 个答案:

答案 0 :(得分:2)

您的目标目录未以双空终止(请参阅SHFILEOPSTRUCT的文档)。将代码更改为:

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