簡體   English   中英

如何在 VB.Net 中使用單個 windows 復制 GUI 復制多個文件/文件夾

[英]How to copy multiple files/folders using single windows copy GUI in VB.Net

我正在 VB.Net 中制作一個應用程序,它將許多文件和文件夾復制到同一目錄,我希望為此使用 windows 資源管理器(因此用戶有 GUI,我不必擔心顯示任何錯誤或比較文件)。

所以,如果我對每個文件/文件夾都這樣做:

My.Computer.FileSystem.CopyDirectory(source_path, target_path, FileIO.UIOption.AllDialogs)
My.Computer.FileSystem.CopyFile(source_path, target_path, FileIO.UIOption.AllDialogs)

它工作正常並顯示此 window:

單份

這很好,但是,如果我有很多文件和/或文件夾,並且我遍歷它們並調用上面的命令,他們會為每個文件/文件夾啟動一個新副本 window,而不是啟動一個將它們組合在一起的單一 GUI,就像這樣:

多個副本

是否可以將多個文件/文件夾復制過程合並到一個 windows 資源管理器副本 window GUI 中?

感謝@Jimi,我被指出了SHFileOperations的方向,所以我想出了如何做到這一點。 我做了一個小的 class 來做到這一點:

Imports System.Runtime.InteropServices

Public Class NativeCopy
    
    Private Enum FO_Func As Short
        FO_COPY   = &H2
        FO_DELETE = &H3
        FO_MOVE   = &H1
        FO_RENAME = &H4
    End Enum

    Private Structure SHFILEOPSTRUCT

        Public hwnd  As IntPtr
        Public wFunc As FO_Func

        <MarshalAs(UnmanagedType.LPWStr)>
        Public pFrom As String

        <MarshalAs(UnmanagedType.LPWStr)>
        Public pTo                   As String
        Public fFlags                As UShort
        Public fAnyOperationsAborted As Boolean
        Public hNameMappings         As IntPtr

        <MarshalAs(UnmanagedType.LPWStr)>
        Public lpszProgressTitle As String

    End Structure

    <DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
    Private Shared Function SHFileOperation(
       <[In]> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
    End Function

    Private Shared _ShFile As SHFILEOPSTRUCT

    Public Shared Sub Copy(ByVal sSource As List(Of String), ByVal sTarget As String)
        _ShFile.wFunc = FO_Func.FO_COPY
        _ShFile.pFrom = String.Join(vbNullChar, sSource) + vbNullChar
        _ShFile.pTo   = sTarget
        SHFileOperation(_ShFile)
   End Sub

End Class

復制文件和/或文件夾就像這樣簡單:

Dim copy_items_paths As List(Of String)
Dim target_path      As String

NativeCopy.Copy(copy_items_paths, target_path)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM