Copy all folder and files from one directory to another in C# and VB.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

Hi! I want copy all data from server to local computer.

private void btnCopy_Click(object sender, EventArgs e)
{
    var local = Path.Combine(@"D:\");
    var remote = Path.Combine(@"\\192.168.1.1\d$\");
    File.Copy(local, remote);
}

 

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago Modified on 3 months ago

Hi PRA,

Check this example. Now please take its reference and correct your code.

C#

protected void btnCopy_Click(object sender, EventArgs e)
{
    var remote = @"\\192.168.1.1\";
    var local = @"D:\";
    // Create all the directories.
    foreach (string dirPath in Directory.GetDirectories(remote, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(remote, local));
    }

    // Copy all the files & Replaces any files with the same name.
    foreach (string newPath in Directory.GetFiles(remote, "*.*", SearchOption.AllDirectories))
    {
        File.Copy(newPath, newPath.Replace(remote, local), true);
    }
}

VB.Net

Protected Sub btnCopy_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim remote = "\\192.168.1.1\"
    Dim local = "D:\"
    ' Create all the directories.
    For Each dirPath As String In Directory.GetDirectories(remote, "*", SearchOption.AllDirectories)
        Directory.CreateDirectory(dirPath.Replace(remote, local))
    Next
    ' Copy all the files & Replaces any files with the same name.
    For Each newPath As String In Directory.GetFiles(remote, "*.*", SearchOption.AllDirectories)
        File.Copy(newPath, newPath.Replace(remote, local), True)
    Next
End Sub