C# NET DirectoryCopy 函数中的 VSS

发布于 2024-11-29 20:50:49 字数 1225 浏览 0 评论 0原文

我正在编写一个创建 RAMDisk 的类(基于类库),由于波动性,每 X 分钟我需要将 RAMDisk 的内容备份到物理位置。建议使用 CopyFileEx,因为显然 .NET 文件复制方法不起作用。

由于某种原因,我在尝试使用 CopyFileEx 时收到无效参数错误。我假设我仍然可以使用此函数中的其余 .NET 方法,但可以使用一些帮助来修复/清理它。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2024-12-06 20:50:49

在这里查看答案:我猜复制解决方案会更干净,并且您基本上在做同样的事情:
递归复制文件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文