0

I am writing a class (based on a class library) that creates a RAMDisk, and every X minutes I need to backup the contents of the RAMDisk to a physical location due to volatility. It was suggested to use CopyFileEx, as apparently the .NET file copy methods do not work.

For some reason I am getting an Invalid Arguements error when trying to use CopyFileEx though. I am assuming that I can still use the rest of the .NET methods in this function, but could just use some help fixing/cleaning it up a bit.

public static void CopyDirectoryVSS(string sourcePath, string targetPath)
{
    // Check if the target directory exists, if not, create it.
    if (Directory.Exists(targetPath) == false)
    {
        Directory.CreateDirectory(targetPath);
    }

    // Copy each file into it’s new directory.
    foreach (string dir in Directory.GetDirectories(sourcePath))
    {
        foreach (string file in Directory.GetFiles(dir, "*.*"))
        {
            Console.WriteLine(@"Copying {0}\{1}", targetPath, file);
            CopyFileEx(file, Path.Combine(target, file), null, 0, 0, 0);
        }
    }

    // Copy each subdirectory using recursion.
    DirectoryInfo sourceDir = new DirectoryInfo(@sourcePath);
    DirectoryInfo TargetDir = new DirectoryInfo(targetPath);

    foreach (DirectoryInfo diSourceSubDir in sourceDir.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir = TargetDir.CreateSubdirectory(diSourceSubDir.Name);
        CopyDirectory(diSourceSubDir, nextTargetSubDir);
    }
}
stivlo
  • 83,644
  • 31
  • 142
  • 199

1 Answers1

0

Check out the answer here: I'm guessing that copy solution would be cleaner and you're essentially doing the same thing: Copying Files Recursively

Community
  • 1
  • 1
crlanglois
  • 3,537
  • 2
  • 14
  • 18
  • Mostly in reference to using the FileInfo directly vs a string representation of the file as a CopyFileEx argument. – crlanglois Oct 03 '11 at 17:27