C# 目录复制不返回错误,但不复制目录

标签 c# directory copy system

我正在编写一个非常小的应用程序,通过添加一些自动化功能将目录从一个地方复制到另一个地方。除了实际的复制方法外,整个应用程序都可以完美运行。最糟糕的是我没有收到任何错误,正如你们大多数人所知,这比收到错误更糟糕。

这里是有问题的方法:

public static bool CopyDir(string sPath, string dPath)
        {
            string[] files = Directory.GetFiles(sPath);
            try
            {
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);

                    foreach (string dirPath in Directory.GetDirectories(sPath, "*",
                        SearchOption.AllDirectories))
                        Directory.CreateDirectory(dirPath.Replace(sPath, dPath));

                    foreach (string newPath in Directory.GetFiles(sPath, "*.*",
                        SearchOption.AllDirectories))
                        File.Copy(newPath, newPath.Replace(sPath, dPath), true);
                }
            } catch // this is no use because the Exception is empty.
            {
                return false;
            }
            return false; //the app keeps executing to here and I don't know why
        }

调试器的变量转储:

  • sPath: "C:\\Users\\Jacob Jewett\\Downloads\\func1"
  • dPath: "C:\\Users\\Jacob Jewett\\AppData\\Roaming\\.minecraft\\saves\\data\\functions\\"
  • 文件:[0] “C:\\Users\\Jacob Jewett\\Downloads\\func1\\main.mcfunction”

Downloads\func1 的文件夹树(sPath):

func1
  main.mcfunction

编辑(下午 2:33):好的,这是我重构的代码,它返回 File in use 错误但不复制任何内容。

   public static bool CopyDir(string sPath, string dPath)
        {
            try
            {
                foreach (string dirPath in Directory.GetDirectories(sPath, "*",
                    SearchOption.AllDirectories))
                    Directory.CreateDirectory(dirPath.Replace(sPath, dPath));

                foreach (string newPath in Directory.GetFiles(dPath, "*.*",
                    SearchOption.AllDirectories))
                    File.Copy(newPath, newPath.Replace(sPath, dPath), true);
                return true;
            }
            catch (UnauthorizedAccessException e)
            {
                MessageBox.Show("Attempt to copy failed. Raw: "+e,"IO Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

编辑(17 月 8 日 29 日下午 12:09):我对方法做了一些调整,现在我得到了 路径访问 [ path] 被拒绝。,总比没有好,但我还没有找到可行的解决方案。我在这里浏览了一些错误,大多数人说在调用 File.Copy() 方法之前放置 File.SetAttributes()。这没有效果。或者,我尝试在完成任何复制之前将 sPath1 的属性 Read-Only 目录设置为 none,这也没有效果。

当前代码:

public static bool CopyDir(string sPath, string dPath)
{
    try
    {
        DirectoryInfo spdi = new DirectoryInfo(sPath);
        spdi.Attributes &= ~FileAttributes.ReadOnly;
        foreach (string dirPath in Directory.GetDirectories(sPath, "*",
            SearchOption.AllDirectories))
            Directory.CreateDirectory(dirPath);

        foreach (string newPath in Directory.GetFiles(dPath, "*.*",
            SearchOption.AllDirectories))
        { 
            File.SetAttributes(dPath, FileAttributes.Normal);
            File.Copy(sPath, newPath);
        }
        Directory.CreateDirectory(dPath);
        return true;
    }
    catch (UnauthorizedAccessException e)
    {
        MessageBox.Show("Attempt to copy failed. (UAC) Raw: "+e,"IO Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }
}

最佳答案

编辑:我重新阅读了您的代码,我认为我遇到了真正的问题。

以下说明足以将所有子目录和文件复制到目标位置。

foreach (string dirPath in Directory.GetDirectories(sPath, "*",
         SearchOption.AllDirectories))
         Directory.CreateDirectory(dirPath.Replace(sPath, dPath));

foreach (string newPath in Directory.GetFiles(sPath, "*.*",
        SearchOption.AllDirectories))
        File.Copy(newPath, newPath.Replace(sPath, dPath), true);

外层的 foreach 是多余的。

修改后的代码:

public static bool CopyDir(string sPath, string dPath)
{
    try
    {
         foreach (string dirPath in Directory.GetDirectories(sPath, "*",
                SearchOption.AllDirectories))
                Directory.CreateDirectory(dirPath.Replace(sPath, dPath));

         foreach (string newPath in Directory.GetFiles(sPath, "*.*",
                SearchOption.AllDirectories))
                File.Copy(newPath, newPath.Replace(sPath, dPath), true);
    }
    catch // this is no use because the Exception is empty.
    {
        return false;
    }

    return false; //the app keeps executing to here and I don't know why
}

也就是说,在 catch block 中对 Exception 做一些事情是一个很好的做法。即使它只是记录它 :) 这样你就可以排除它是否没有记录。

关于C# 目录复制不返回错误,但不复制目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924149/

相关文章:

c# - ASP.NET 中的 Gridview 排序

c# - 使用使用 int 参数调用的 Action 初始化任务

c# - 有什么方法可以用 ClrMD 获取局部变量的值(比如 sosex !mdv)?

javascript - 使用 webkitdirectory 和目录上传文件夹不适用于 safari 浏览器

c - 为什么编译后的代码在 linux 的 windows 中创建损坏的文件工作正常

sql - PostgreSQL 将数据从一个数据库复制/传输到另一个数据库

c# - 不可变与可变 C#

dart - 在 dart 中是否有 "correct"文件夹放置测试资源?

c - lstat 返回 <0

python - 如何找到具有相同 ID 的所有变量?