问题
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!
回答1:
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
回答2:
Also, try:
DirectoryInfo di=new DirectoryInfo(@"\\<server>\<share>");
Specific point being the @ symbol; this works on my local network.
回答3:
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
回答4:
can you pass into the function the unc pathing of the folder instead? \\servername\physical path to folder?
来源:https://stackoverflow.com/questions/1266330/copy-whole-shared-directory-from-network