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

问题描述:

我正在写一个非常小的应用程序从一个地方复制目录到另一个与一些额外的自动化。整个应用程序完美地工作,除了实际的复制方法。最糟糕的是我没有得到任何的错误,而且正如大多数人所知道的那样,这比得到错误要糟糕得多。C#目录复制返回没有错误,但不复制目录

这里是有问题的方法:

  • SPATH:"C:\\Users\\Jacob Jewett\\Downloads\\func1"
  • dPath:"C:\\Users\\Jacob Jewett\\AppData\\Roaming\\.minecraft\\saves\\data\\functions\\"
  • 文件:

    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 
         } 
    

    从调试器变量转储[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):我已经做了一些调整方法和我在一个点上我,我现在得到一个Access to path [path] is Denied.,这是比什么都没有,只有更好我还没有遇到过工作解决方案。我在这里做了一些浏览错误,大多数人在调用File.Copy()方法之前先说File.SetAttributes()。这没有效果。另外,我尝试在完成任何复制之前将sPath1Read-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; 
    } 
} 
+2

它很可能在'catch'块内触发'return false'。你遇到了一个例外,但它只是被吞下,你永远不会看到错误。 –

+0

我曾尝试添加更好的catch块,并且异常不会返回任何内容。它甚至不能从我所能看到的捕获块中去除。 –

+0

你是否尝试过使用调试器? – itsme86

编辑:我重读你的代码,我想我已经得到了真正的问题。

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

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块中的例外。即使它只是记录它:)这样,如果它不记录,你可以排除它。

+0

'string [] files'不是空的。调试器显示它包含源目录中的所有正确文件'main.mcfunction'。 –

+0

对,我的不好:S编辑我的答案以消除这个假设。但是代码仍然适用于我的目标,所以它必须是别的。 sPath的文件夹结构是什么样的? –

当你说你没有得到任何错误,做文件复制?如果是这样,那么也许你只是忘记在try块的外部foreach循环结束时返回true。

否则,此代码为我完美地复制了一个文件夹结构。

try{ 
    foreach(...) 
    { 
     // etc 
    } 

    return true;  // <<<<<<-------- 
} 
catch 
{ 
} 
return false; 

另外,如果你在主界面线程,然后运行这个它会锁定东西和调试器可能会抱怨上下文切换死锁,所以一定要确保你在一个线程中运行它。

就使用调试器而言,在您要停止的位置放置一个断点,然后使用F10跳过一条直线,然后使用F11跳入一条直线。