2011-08-13 116 views
1

這可能是一個問題,甚至沒有人知道我在說什麼......但在這裏。更改pinvoke SHFileOperation根目錄

因此,我想使用實際的Windows確認對話框刪除,複製,移動和重命名文件。 我正在使用。我的問題是,是,當我想以一個文件夾移動這個路徑:
「C:\ MyFolderToMove」
我目的地設定爲:
「C:\用戶\測試」
(THIS部分很重要)從發言權應用開始
「d:\ MyApp.exe的」
SHFileOperation會嘗試將其移動到:
「d:\ C \用戶\測試」

所以,它基本上結合了應用程序的「開始」文件夾與您指定的目的地。

是否有人有如何解決這個問題的想法?
這是代碼,以防萬一有人想看到它。 (這是在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

您摸索了DestinationDirectory參數的值。目前還不清楚,它不在片段中。考慮使用My.Computer.FileSystem.MoveDirectory代替。 –

+0

謝謝您的評論,但我完全相信我正在發送正確的路徑。我甚至沒有動態設置它,只是付了一個靜態字符串。至於使用其他功能,那個人一次只能移動一個目錄,我稍後需要移動多個文件和目錄。 –

+0

「我完全相信我正在發送正確的道路。」好吧,顯然你不是。如果你是那麼功能會起作用。再看一遍。 –

回答

2

你的目標目錄不是雙空終止(見文檔SHFILEOPSTRUCT)。將代碼更改爲:

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