Copying folder using windows API

gabru

Active member
Joined
Feb 19, 2004
Messages
36
Location
Austria / Vienna
i want to copy a folder inclusive all subfolders and all files to a specified location. How do i do that? I dont want to code this myself if there is a possibility to use an existing component.

The best way for me would be to use the copy-dialog which the windows-explorer use because there is everything handled. all exceptions, etc. is there a way i can use this? thanks!
 
if you want to move an entire directory without a loop , you can use the filesystemobject class.
heres a quick example ...
Code:
        Dim tpFolder As Type = Type.GetTypeFromProgID("Scripting.FileSystemObject")

        Dim objFolder As Object = Activator.CreateInstance(tpFolder)

        Dim objArgs As Object() = {"C:\testing", "F:\testing", True} /// C:\ existing path , F:\ path to copy to , overwrite if exists on F:\ already

        tpFolder.InvokeMember("CopyFolder", Reflection.BindingFlags.InvokeMethod, Nothing, objFolder, objArgs)

        objFolder = Nothing
 
thanks for it but i think its not the right solution for me because i need the explorers copy-dialog. i also want to execute the explorers copy-function ...
so i think i need to use the windows api .. can anyone help?
 
if you want to use the api way , heres a quick example i put together ( NOTE : if you want a silent copy , ie: NO progress dialog add FOF_SILENT for the fFlags of the structure ) ...
Code:
    Public Structure SHFILEOPSTRUCT
        Public hWnd As Integer
        Public wFunc As Integer
        Public pFrom As String
        Public pTo As String
        Public fFlags As Integer
        Public fAborted As Integer
        Public hNameMaps As Integer
        Public sProgress As String
    End Structure

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

    Private Const FO_COPY As Int32 = &H2
    Private Const FO_DELETE As Int32 = &H3
    Private Const FO_MOVE As Int32 = &H1
    Private Const FOF_SILENT As Int32 = &H4 /// this will hide the progress box ( do NOT use if you wish to see progress )

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim shStructure As New SHFILEOPSTRUCT()
        Dim currentpath As String = "C:\testing" /// folder to copy
        Dim newpath As String = "F:\testing" /// new path

        With shStructure
            .wFunc = FO_COPY
            .pFrom = currentpath
            .pTo = newpath
        End With

        SHFileOperation(shStructure)

    End Sub
 
Back
Top