# Copy Directory Keeps throwing Exceptions :[



## <<Onafets>> (Dec 7, 2010)

Heres mai 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:


```
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]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 :[


----------



## Kreij (Dec 7, 2010)

What exception is it throwing?


----------



## <<Onafets>> (Dec 7, 2010)

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)


----------



## Kreij (Dec 7, 2010)

That's becuase there is no public method for CopyDirectory that takes 5 arguments.
http://msdn.microsoft.com/en-us/library/2swy9y5e(VS.80).aspx


----------



## Gzero (Dec 7, 2010)

<<Onafets>> said:


> 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.


----------



## <<Onafets>> (Dec 7, 2010)

Any ideas on how to do that? Sorry I am still learning to program in .NET :[


----------



## Kreij (Dec 12, 2010)

Sorry, Onafets, I forgot about this thread. 
Did you get this worked out or our you still looking for assistance?


----------



## FordGT90Concept (Dec 12, 2010)

I couldn't find any good code for a recusive copy to point you to so I decided to write my own...


```
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...

```
My.Computer.FileSystem.CopyDirectory(folder, newlocation, True)
```
...that is effectively the same as...

```
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.


----------



## <<Onafets>> (Dec 13, 2010)

Thank you for all the help! I kinda fixed it and instead of using dialog boxes i used notification icons  I'm actually working on a minecraft backup utility I can post a link if you would like to help me test for bugs?


----------

