私はC#.NETのSystem.IO名前空間で作業するのにちょっと新しいです。だから、いくつかの基本的な質問について私を許してください。System.IO.Directory.Delete(pathtodelete、true)が「ディレクトリが空ではない」というエラーを表示しないようにするにはどうすればよいですか?
サイトのオーナーがサーバー上のファイルやディレクトリを変更できるようにするオンラインインターフェイスを作成しています。
System.IO.Directory.Delete(PathToDelete, true);
のうち、矛盾したパフォーマンスが発生しました。時々それは素晴らしい、時にはエラーをスローします。言うことのディレクトリを削除するトリングとき時々、私はエラーを取得する
public ActionResult FileDelete(List<string> entity = null)
{
if (entity != null)
{
if (entity.Count() > 0)
foreach (string s in entity)
{
string CurrentFile = s.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string FileToDelete = Server.MapPath(CurrentFile);
bool isDir = (System.IO.File.GetAttributes(FileToDelete) & FileAttributes.Directory) == FileAttributes.Directory;
if (isDir)
{
if (System.IO.Directory.Exists(FileToDelete))
{
//Problem line/////////////////////////////////
System.IO.Directory.Delete(FileToDelete, true);
}
}
else
{
if (System.IO.File.Exists(FileToDelete))
{
System.IO.File.Delete(FileToDelete);
string ThumbConfigDir = ConfigurationManager.AppSettings["ThumbnailSubdirectory"];
string ThumbFileToDelete = Path.GetDirectoryName(FileToDelete) + Path.DirectorySeparatorChar + ThumbConfigDir + Path.DirectorySeparatorChar + Path.GetFileName(FileToDelete);
if (System.IO.File.Exists(ThumbFileToDelete))
{
System.IO.File.Delete(ThumbFileToDelete);
}
}
}
}
}
return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri.ToString());
}
:
The directory is not empty.
Description: An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about the error
and where it originated in the code.
Exception Details: System.IO.IOException: The directory is not empty.
Source Error:
Line 137: if (System.IO.Directory.Exists(FileToDelete))
Line 138: {
Line 139: System.IO.Directory.Delete(FileToDelete, true);
Line 140: }
Line 141: }
私は私が手を避けるために実装することができ、防御コーディングのどのようなわからないんだけど私のコントローラは、このようになりますこれらのようなエラー。何かご意見は? System.IO.Directory.Delete(FileToDelete, true);
と言って、再帰的にtrueに設定することが何を意味するのか迷っていますか?
削除(PathToDelete、true)を使用している場合は削除できません。オプションは何ですか、削除した内容は何ですか? – Bojangles
@Bojangles - これは悪い考えですが、このファイル/フォルダを使用するすべてのハンドル/プログラムを強制終了し、削除する可能性があります(ボックス管理者権限が必要になる可能性があります)... "Windows Internals" (ほとんどすべてのエディション)は良い出発点です。 –