2017-08-28 10 views
2

私は非常に小さなアプリケーションを作成しています。実際のコピー方法を除いて、アプリケーション全体が完璧に動作します。最悪の部分はまったくエラーがないことです。ほとんどの人が知っているように、エラーを取得するよりもかなり悪いです。ここでC#ディレクトリコピーでエラーは返されませんが、ディレクトリはコピーされません

問題の方法です:デバッガから

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"

フォルダツリー(SPATH):

func1 
    main.mcfunction 

EDIT(14:33):OKだからここに私のリファクタリング、コードがだ、それは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; 
      } 
     } 

EDIT(8-29-17 12:09):私はメソッドにいくつかの調整を行ってきたと私は何もないよりはましです私は今Access to path [path] is Denied.を取得する時点で、だけど私はまだ実用的な解決策を見つけていない。私はいくつかここでエラーを参照して、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; 
    } 
} 
+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ループの外側で真を返すことを忘れているかもしれません。

そうでなければ、このコードはフォルダ構造を完全に複製しました。

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

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

あなたがメインのGUIスレッドでこれを実行している場合も、物事をロックするだろうと、デバッガはおそらくコンテキストに文句を言うだろうが死んでロックを切り替えるので、あなたがスレッドでそれを実行していることを確認してください。

デバッガの使用に関しては、停止させるブレークポイントを設定し、F10を使用して行をステップオーバーし、F11を使用して行にステップインします。

関連する問題