none
copy a folder and subfolders + files from remote location to local drive

    Question

  • Hi,

    i am making a windows application( C#.Net) in which there is a folder placed at remote location( on other computer). this folder contains files + subfolders inside it. i need to copy all the files and subfolders present inside this main folder to my local drive. How can i do this. 

    Thanks in advance

    Thursday, October 07, 2010 7:06 AM

Answers

  • hi Chk this out. Here is a simple way..

    Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
    
        Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
    
        Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
    
    
    
        ' the source directory must exist, otherwise throw an exception
    
        If SourceDir.Exists Then
    
          ' if destination SubDir's parent SubDir does not exist throw an exception
    
          If Not DestDir.Parent.Exists Then
    
            Throw New DirectoryNotFoundException _
    
              ("Destination directory does not exist: " + DestDir.Parent.FullName)
    
          End If
    
    
    
          If Not DestDir.Exists Then
    
            DestDir.Create()
    
          End If
    
    
    
          ' copy all the files of the current directory
    
          Dim ChildFile As FileInfo
    
          For Each ChildFile In SourceDir.GetFiles()
    
            If Overwrite Then
    
              ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)
    
            Else
    
              ' if Overwrite = false, copy the file only if it does not exist
    
              ' this is done to avoid an IOException if a file already exists
    
              ' this way the other files can be copied anyway...
    
              If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then
    
                ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), False)
    
              End If
    
            End If
    
          Next
    
    
    
          ' copy all the sub-directories by recursively calling this same routine
    
          Dim SubDir As DirectoryInfo
    
          For Each SubDir In SourceDir.GetDirectories()
    
            CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, _
    
              SubDir.Name), Overwrite)
    
          Next
    
        Else
    
          Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
    
        End If
    
      End Sub

    Sreenath G V. Mark as answer/vote as helpful if it helped you
    • Marked as answer by Jing0Moderator Thursday, October 14, 2010 3:24 AM
    Tuesday, October 12, 2010 6:06 AM
  • File.Copy only copies files. no built-in method in .NET to copy folder, but you can do that easily by going through the files in the folder.

     

    here is an example:

    http://msdn.microsoft.com/en-us/library/cc148994.aspx

     

    If you need to copy sub folders as well, check out this example:

    http://www.csharp411.com/c-copy-folder-recursively/


    Eli Gazit www.softwear.co.il
    • Proposed as answer by Eli Gazit Monday, October 11, 2010 6:31 AM
    • Marked as answer by Jing0Moderator Thursday, October 14, 2010 3:24 AM
    Thursday, October 07, 2010 10:04 AM

All replies

  • You will a client installed on the remote computer that is open for communication from the local computer, or via a server that will get the files from the remote. the question is what your system architecture? does both computer in the same LAN? or these are computers over the internet?

     


    Eli Gazit www.softwear.co.il
    Thursday, October 07, 2010 7:12 AM
  • Hi,

    Thanks for the reply.Both of the computers are on the same LAN. i just need to copy the files and subfolders inside the main folder to my local drive using C#.Net. 

    Thursday, October 07, 2010 7:17 AM
  • In this case, just map the remote computer as a network folder on the local computer, and do a simple file-system copy (File.Copy(...)).

     


    Eli Gazit www.softwear.co.il
    • Marked as answer by Trivedi.Pooja Thursday, October 07, 2010 7:50 AM
    • Unmarked as answer by Trivedi.Pooja Thursday, October 07, 2010 9:33 AM
    Thursday, October 07, 2010 7:26 AM
  • Thanks for the answer, just one more question please -Will the file.copy copies the whole folder or a certain file only?
    Thursday, October 07, 2010 7:33 AM
  • Hi,

    i tried file.copy but it is giving me login failure error. Do i need to provide and login credentials before file.copy? if yes the how can i do that. Please help

    Thursday, October 07, 2010 7:56 AM
  • If you can copy via the file system, File.Copy should work as well. Can you?
    Eli Gazit www.softwear.co.il
    Thursday, October 07, 2010 8:10 AM
  • i am able to copy it via file system . using run and then typing the ip address of remote server but when doing through asp.net. it is throwing me login failure exception. i know the reason for this. Actually while doing copy through file system, i enter a different user name and password but while doing through .net, it is taking my default login and password which does not have access to that file. So i want to provide that user name and password which has access to the remote folder. can i do it in my program? thanks
    Thursday, October 07, 2010 8:28 AM
  • can some one please help me with this. i urgently require this code and not able to copy files from remote system. Searched everywhere in msdn but did not  found any solution till yet.
    Thursday, October 07, 2010 8:58 AM
  • If you are doing that in ASP.NET, you need to ask on a different forum, this is a windows-forms forum.

    This is because coping files in asp.net is a different story - as you find out, there are permission issues - you can't access the 'server' file system via any web-page - you need to set for the user IO permissions on the IIS, etc. this is not a simple task and you need to consult with experts from that field.

     


    Eli Gazit www.softwear.co.il
    Thursday, October 07, 2010 9:11 AM
  • file.copy does not copy the folders or directory. i tried using file.copy but gave me an error. i used the following code:

    File.Copy(@"Y:\EHRO", @"C:\Pooja");

    The error coming is:   The target file "C:\Pooja" is a directory, not a file.

    Thursday, October 07, 2010 9:36 AM
  • File.Copy only copies files. no built-in method in .NET to copy folder, but you can do that easily by going through the files in the folder.

     

    here is an example:

    http://msdn.microsoft.com/en-us/library/cc148994.aspx

     

    If you need to copy sub folders as well, check out this example:

    http://www.csharp411.com/c-copy-folder-recursively/


    Eli Gazit www.softwear.co.il
    • Proposed as answer by Eli Gazit Monday, October 11, 2010 6:31 AM
    • Marked as answer by Jing0Moderator Thursday, October 14, 2010 3:24 AM
    Thursday, October 07, 2010 10:04 AM
  • Hi Trivedi.Pooja,

     

    Eli Gazit’s reply is helpful.

     

    How is this problem now?

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, October 11, 2010 8:56 AM
  • hi Chk this out. Here is a simple way..

    Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
    
        Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
    
        Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
    
    
    
        ' the source directory must exist, otherwise throw an exception
    
        If SourceDir.Exists Then
    
          ' if destination SubDir's parent SubDir does not exist throw an exception
    
          If Not DestDir.Parent.Exists Then
    
            Throw New DirectoryNotFoundException _
    
              ("Destination directory does not exist: " + DestDir.Parent.FullName)
    
          End If
    
    
    
          If Not DestDir.Exists Then
    
            DestDir.Create()
    
          End If
    
    
    
          ' copy all the files of the current directory
    
          Dim ChildFile As FileInfo
    
          For Each ChildFile In SourceDir.GetFiles()
    
            If Overwrite Then
    
              ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)
    
            Else
    
              ' if Overwrite = false, copy the file only if it does not exist
    
              ' this is done to avoid an IOException if a file already exists
    
              ' this way the other files can be copied anyway...
    
              If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then
    
                ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), False)
    
              End If
    
            End If
    
          Next
    
    
    
          ' copy all the sub-directories by recursively calling this same routine
    
          Dim SubDir As DirectoryInfo
    
          For Each SubDir In SourceDir.GetDirectories()
    
            CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, _
    
              SubDir.Name), Overwrite)
    
          Next
    
        Else
    
          Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
    
        End If
    
      End Sub

    Sreenath G V. Mark as answer/vote as helpful if it helped you
    • Marked as answer by Jing0Moderator Thursday, October 14, 2010 3:24 AM
    Tuesday, October 12, 2010 6:06 AM