1

In my app i need to copy entire XDCam Disks (prof. Broadcast Record Disk) to our fileserver. But following function only seems to wirk if the source drive is not a root directory.

Not working

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

Working

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

How can i copy the entire disk to my target directory?

  • for all files and directories copy files, and do the filesyste.copydirectory for directories? – BugFinder May 11 '16 at 13:29
  • 1
    What is it doing when it is "not working"? Is there an error? Is it doing absolutely nothing at all and continuing with whatever code is next? – Broots Waymb May 11 '16 at 13:33
  • This may be a permissions issue - do you **need** to be able to copy from the root? – Bassie May 11 '16 at 13:33
  • Do you expect all folders to be copied - that method is not recursive. If so [Best way to copy the entire contents of a directory in C#](http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp) – Alex K. May 11 '16 at 13:34
  • According to the Docs - attempting to copy the root directory will cause an IOException - so you will probably have to copy all folders from the root separately – PaulF May 11 '16 at 13:42

2 Answers2

1

This is how I did it in the past:

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);
    }
}

With progress you could do something like

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
}

works best with small files

Jan Wiesemann
  • 455
  • 2
  • 16
  • That works fine. Only thing is that i don't have progress bar anymore. So that will be the next thing to implement. I need a progressbar because older XDCam Drives are USB 2.0 only and the disk can contain up to 50GB. So i need something to monitor the copy process. Thanks! – Michael Jones May 11 '16 at 14:05
0

Instead of targeting the root directory, try getting all the subdirectories of the root and copy them individually:

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