2011-01-21 6 views
2

私は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に設定することが何を意味するのか迷っていますか?

答えて

1

使用中のファイルがある場合、削除はディレクトリを空にしませんし、ディレクトリを削除しようとすると失敗します。

スタティックメソッドの代わりにFileInfoを使用して、ファイルに対して何らかの操作を実行した後に[更新]を使用してください。 (またはDirectoryInfo direcotriesのための)一般的に

Similar problem

0

あなただけのファイル/フォルダの操作コードから例外がこの種のを期待しなければなりません。いくつかのファイルは使用中のファイル、ディレクトリに作業フォルダが設定されているもの、権限のためにプロセスに表示されないものがあります。

プロセスモニタ(http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx)が原因で問題が発生する可能性があります。

一時ファイル用に自分でフォルダを作成して削除しようとすると、そのようなフォルダにあるファイルに関連するStreamオブジェクトを処分することを忘れた場合(ReaderとWriterオブジェクト、XmlDocumentによる間接リンク)

+0

削除(PathToDelete、true)を使用している場合は削除できません。オプションは何ですか、削除した内容は何ですか? – Bojangles

+0

@Bojangles - これは悪い考えですが、このファイル/フォルダを使用するすべてのハンドル/プログラムを強制終了し、削除する可能性があります(ボックス管理者権限が必要になる可能性があります)... "Windows Internals" (ほとんどすべてのエディション)は良い出発点です。 –

関連する問題