tags:

views:

402

answers:

4

Hi
I am trying to copy whole directory tree from server's shared folder to my local machine, I found Best way to copy the entire contents of a directory in C# post and decide to use that but as I guess DirectoryInfo doesn't support network shares, how can I change this code to work with network shares as a source?

public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) {
    foreach (DirectoryInfo dir in source.GetDirectories())
        CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
    foreach (FileInfo file in source.GetFiles())
        file.CopyTo(Path.Combine(target.FullName, file.Name));
}

EDIT
and the call is

CopyFilesRecursively(new DirectoryInfo ("\\192.168.0.11\Share"), new DirectoryInfo ("D:\Projects\"));

and getting error message

Could not find a part of the path 'D:\192.168.0.11\Share'.

Thanks a lot!

A: 

can you pass into the function the unc pathing of the folder instead? \\servername\physical path to folder?

klabranche
A: 

If it doesn't support the UNC path, you could map the network share to a drive letter. Not terribly portable if you need to do this for a user specified share; but would work if you only need to focus on one share.

on XP: My Computer -> Tools -> Map Network Drive

CoderTao
+1  A: 

Also, try:

DirectoryInfo di=new DirectoryInfo(@"\\<server>\<share>");

Specific point being the @ symbol; this works on my local network.

CoderTao
+2  A: 

What about escaping the strings?

CopyFilesRecursively(
    new DirectoryInfo(@"\\192.168.0.11\Share"),
    new DirectoryInfo(@"D:\Projects\"));

MSDN says that DirectoryInfo can handle UNC paths

The Chairman