copy whole shared directory from network

為{幸葍}努か 提交于 2019-12-07 09:31:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!