1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Copy Directory Keeps throwing Exceptions :[

Discussion in 'Programming & Webmastering' started by <<Onafets>>, Dec 7, 2010.

  1. <<Onafets>>

    <<Onafets>>

    Joined:
    Nov 8, 2008
    Messages:
    779 (0.36/day)
    Thanks Received:
    77
    Location:
    Sydney, Australia
    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 :[
     
  2. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    What exception is it throwing?
     
  3. <<Onafets>>

    <<Onafets>>

    Joined:
    Nov 8, 2008
    Messages:
    779 (0.36/day)
    Thanks Received:
    77
    Location:
    Sydney, Australia
    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)
     
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
  5. Gzero New Member

    Joined:
    Mar 16, 2005
    Messages:
    718 (0.21/day)
    Thanks Received:
    52
    Location:
    Scotland
    You will need to write a class that overrides that class function, if you want to do it the way your trying.
     
  6. <<Onafets>>

    <<Onafets>>

    Joined:
    Nov 8, 2008
    Messages:
    779 (0.36/day)
    Thanks Received:
    77
    Location:
    Sydney, Australia
    Any ideas on how to do that? Sorry I am still learning to program in .NET :[
     
  7. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Sorry, Onafets, I forgot about this thread.
    Did you get this worked out or our you still looking for assistance?
     
  8. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,559 (6.25/day)
    Thanks Received:
    3,495
    Location:
    IA, USA
    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: Dec 12, 2010
    Crunching for Team TPU
  9. <<Onafets>>

    <<Onafets>>

    Joined:
    Nov 8, 2008
    Messages:
    779 (0.36/day)
    Thanks Received:
    77
    Location:
    Sydney, Australia
    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?
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page