从网络复制整个共享目录 [英] copy whole shared directory from network

查看:179
本文介绍了从网络复制整个共享目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将整个目录树从服务器的共享文件夹复制到我的本地机器,我发现最好的方法复制一个目录在C#的全部内容并决定使用,但我猜DirectoryInfo不支持网络共享,如何

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

EDIT

并且调用是

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

并收到错误讯息

 找不到路径D:\ 192.168.0.11\Share的一部分。非常感谢!



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

MSDN说 DirectoryInfo可以处理UNC路径


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!

解决方案

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

这篇关于从网络复制整个共享目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆