• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Copy Directory Keeps throwing Exceptions :[

Joined
Nov 8, 2008
Messages
779 (0.13/day)
Location
Sydney, Australia
System Name Gearbox || Server
Processor i5 3570K @ 4.0Ghz || E8400 @ Stock 3Ghz
Motherboard Gigabyte Z68XP-UD3 || Gigabyte EP41-UD3L
Cooling Stock || Stock
Memory 8GB G.Skill RipjawX DDR3 @ 1600mhz || 4GB Kingston Value DDR2 800Mhz
Video Card(s) ASUS R9 270X Direct CU II TOP @ 1120/1500 || N/A
Storage Samsung 840 EVO 250GB || 1TB WD Green, 2TB WD Green, 3TB WD Red
Display(s) HP x23 LED 23" Full HD Panel
Case Corsair 200R || Open-Air
Audio Device(s) Audioengine D1 + Logitech Z623/Audio Technica ATH-M50 || N/A
Power Supply Antec EarthWatts Platinum 650 W || Antec Neo Eco 450 W
Software Windows 8.1 Update 3 Pro 64 || Ubuntu Server 14.04 64
Heres mai code:

Code:
If CheckBox1.Checked Then
            My.Computer.FileSystem.CopyDirectory(folder, newlocation, FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)

I wanted to add a boolean so it would automatically overwrite the directory like so:

Code:
If CheckBox1.Checked Then
            My.Computer.FileSystem.CopyDirectory(folder, newlocation, FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing, True)

But it wont accept it :[

How can i copy a folder so it automatically overwrites while showing the copy boxes?

FYI I have already tried this:

Code:
[CODE]If CheckBox1.Checked Then
            My.Computer.FileSystem.CopyDirectory(folder, newlocation, FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing, True)
On Error Resume Next

And there are End if's lol

This is really puzzling me :[
 
What exception is it throwing?
 
Overload resolution failed because no accessible 'CopyDirectory' accepts this number of arguments.

When I try My.Computer.FileSystem.CopyDirectory(folder, newlocation, FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing, True)
 
Overload resolution failed because no accessible 'CopyDirectory' accepts this number of arguments.

When I try My.Computer.FileSystem.CopyDirectory(folder, newlocation, FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing, True)

You will need to write a class that overrides that class function, if you want to do it the way your trying.
 
Any ideas on how to do that? Sorry I am still learning to program in .NET :[
 
Sorry, Onafets, I forgot about this thread.
Did you get this worked out or our you still looking for assistance?
 
I couldn't find any good code for a recusive copy to point you to so I decided to write my own...

Code:
Imports System.IO

Module StaticMethods

    ''' <summary>
    ''' Recursively copies a directory and files to a new location.
    ''' </summary>
    ''' <param name="source">The folder name to copy.</param>
    ''' <param name="destination">The name of the folder to copy to (will be created if not exist).</param>
    ''' <param name="recursive">Whether or not to copy sub directories.</param>
    ''' <param name="overwrite">Whether or not to overwrite files.</param>
    ''' <param name="delete_dest_before_copy">Whether or not to delete the destination directory before copying (ensures a 1:1 copy).</param>
    ''' <returns>An exception or Nothing if there were no problems.</returns>
    Public Function CopyDirectory(ByVal source As String, ByVal destination As String, Optional ByVal recursive As Boolean = True, Optional ByVal overwrite As Boolean = True, Optional ByVal delete_dest_before_copy As Boolean = False) As Exception
        Try
            ' Delete the destination directory if the option is true.
            If delete_dest_before_copy Then
                If Directory.Exists(destination) Then Directory.Delete(destination, True)
            End If

            ' Get information about the source directory and destination directory.
            Dim source_info As DirectoryInfo = New DirectoryInfo(source)
            Dim dest_info As DirectoryInfo = New DirectoryInfo(destination)

            ' Make sure the directory exists.
            dest_info.Create()

            ' If set to act recursively, perform the option for all sub directories.
            If recursive Then
                For Each dir As DirectoryInfo In source_info.GetDirectories()
                    CopyDirectory(dir.FullName, Path.Combine(dest_info.FullName, dir.Name), recursive, overwrite, False)
                Next
            End If

            ' Copy all files in this directory.
            For Each file As FileInfo In source_info.GetFiles()
                file.CopyTo(Path.Combine(dest_info.FullName, file.Name), overwrite)
            Next
        Catch ex As Exception
            Return ex ' Return the exception error.
        End Try
        Return Nothing ' No errors.
    End Function

    Sub Main()
        ' Testing the code.
        Dim result As Exception = StaticMethods.CopyDirectory("C:\Users\Admin\Desktop\Chess", "C:\Users\Admin\Desktop\Copy\Chess", True, True)
        If Not result Is Nothing Then
            Console.Write(result.ToString())
            Console.ReadKey()
        End If
    End Sub

End Module

All it requires to work is CopyDirectory(source, destination). The other arguments are optional.


Alternatively, you could do...
Code:
My.Computer.FileSystem.CopyDirectory(folder, newlocation, True)
...that is effectively the same as...
Code:
CopyDirectory(folder, newlocation)
...but I'm pretty sure My.Computer is a VB only thing so it's a bad habit to get into using that.
 
Last edited:
Thank you for all the help! I kinda fixed it and instead of using dialog boxes i used notification icons :D I'm actually working on a minecraft backup utility I can post a link if you would like to help me test for bugs?
 
Back
Top