C#将整个可移动磁盘复制到共享文件夹

时间:2016-05-11 13:27:44

标签: c# copy filesystems directory

在我的应用程序中,我需要将整个XDCam磁盘(教授广播记录磁盘)复制到我们的文件服务器。但是,如果源驱动器不是根目录,则跟随功能似乎只是推迟。

不工作

    FileSystem.CopyDirectory("E:\\", serverPath + this.targtetDirectory + "\\", UIOption.AllDialogs);

工作

FileSystem.CopyDirectory("E:\\Clip\\", serverPath + this.targtetDirectory + "\\", UIOption.AllDialogs);

如何将整个磁盘复制到目标目录?

2 个答案:

答案 0 :(得分:1)

这就是我过去的做法:

void CopyAppFiles(string SourcePath, string )
{
    foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
    }

    foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
    {
        File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
    }
}

随着进步,您可以做类似

的事情
void CopyAppFiles(string SourcePath, string )
{
    string[] dirs = Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories);
    string[] files = Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories)

    int totalOperations = dirs.length + files.length;

    for(int i = 0; i < dirs.length; i++)
    {
        Directory.CreateDirectory(dirs[i].Replace(SourcePath, DestinationPath));

        ReportProgress(i, totalOperations);
    }

    for(int i = 0; i < files.length; i++)
    {
        File.Copy(files[i], files[i].Replace(SourcePath, DestinationPath), true);

        ReportProgress(i + dirs.length, totalOperations);
    }
}

void ReportProgress(int currentOperation, int totalOperations)
{
    //TODO
}

最适合小文件

答案 1 :(得分:0)

尝试获取根目录的所有子目录并单独复制它们,而不是定位根目录:

string destinationDir = serverPath + this.targetDirectory + @"\";
foreach (var file in Directory.GetFiles(@"E:\", "*.*", System.IO.SearchOption.AllDirectories))
{
    File.Copy(file, destinationDir + file.Substring(targetDirectory.Length));
}