私は非常に小さなアプリケーションを作成しています。実際のコピー方法を除いて、アプリケーション全体が完璧に動作します。最悪の部分はまったくエラーがないことです。ほとんどの人が知っているように、エラーを取得するよりもかなり悪いです。ここで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;
}
}
'catch'ブロック内で' return false'を打つ可能性が非常に高いです。あなたは例外に当たっていますが、それは単に飲み込まれており、エラーは決して見られません。 –
私はより良いcatchブロックを追加しようとしましたが、例外は何も返しません。私が見ることができるところからキャッチブロックに行くことさえありません。 –
デバッガでステップアップしてみましたか? – itsme86