2017-08-28 73 views
2

我正在寫一個非常小的應用程序從一個地方複製目錄到另一個與一些額外的自動化。整個應用程序完美地工作,除了實際的複製方法。最糟糕的是我沒有得到任何的錯誤,而且正如大多數人所知道的那樣,這比得到錯誤要糟糕得多。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

回答

0

編輯:我重讀你的代碼,我想我已經得到了真正的問題。

以下說明足以將所有子目錄和文件複製到目標。

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的文件夾結構是什麼樣的? –

0

當你說你沒有得到任何錯誤,做文件複製?如果是這樣,那麼也許你只是忘記在try塊的外部foreach循環結束時返回true。

否則,此代碼爲我完美地複製了一個文件夾結構。

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

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

另外,如果你在主界面線程,然後運行這個它會鎖定東西和調試器可能會抱怨上下文切換死鎖,所以一定要確保你在一個線程中運行它。

就使用調試器而言,在您要停止的位置放置一個斷點,然後使用F10跳過一條直線,然後使用F11跳入一條直線。