2016-04-23 11 views
1

私はこのエラーが発生しました管理者としてプログラムを実行しようとしましたが、まだこのエラーが出ません最近のドキュメントフォルダのショートカットを消去できない理由はありません。 :C#許可されていない例外

//this will delete the the files in the Recent Documents directory 
private void DeleteRecentDocuments(string RecentDocumentsDirectory) 
{ 
    //this is the directory and parameter which we will pass when we call the method 
    DirectoryInfo cleanRecentDocuments = new DirectoryInfo(RecentDocumentsDirectory); 

    //try this code 
    try 
    { 
     //loop through the directory we use the getFiles method to collect all files which is stored in recentDocumentsFolder variable 
     foreach(FileInfo recentDocumentsFolder in cleanRecentDocuments.GetFiles()) 
     { 
      //we delete all files in that directory 
      recentDocumentsFolder.Delete(); 
     } 
    } 
    //catch any possible error and display a message 
    catch(Exception) 
    { 
     MessageBox.Show("Error could not clean Recent documents directory, please try again"); 
    } 
} 

は、私は上記のこの方法が、あまりにも多くのそのだけのメソッドを呼び出し、パラメータがディレクトリであるDW試合を呼び出します。あなたが望むなら私はそれを投稿することができます。

+0

役立つ

private static void DeleteRecentDocuments(string RecentDocumentsDirectory) { //this is the directory and parameter which we will pass when we call the method DirectoryInfo cleanRecentDocuments = new DirectoryInfo(RecentDocumentsDirectory); //try this code try { //loop through the directory we use the getFiles method to collect all files which is stored in recentDocumentsFolder variable foreach (FileInfo recentDocumentsFolder in cleanRecentDocuments.GetFiles()) { //we delete all files in that directory File.Delete(RecentDocumentsDirectory + recentDocumentsFolder); } } //catch any possible error and display a message catch (Exception ex) { Console.WriteLine(ex.Message); } } 

希望をテストしているコピーされます例外は何ですか? RecentDocumentsDirectory = @ "C:\ Users \ username \ Documents \ディレクトリ" –

答えて

2

MSDNによると、FileInfo.Delete()UnauthorizedAccessException

foreach (string filePath in Directory.GetFiles(recentDocumentsFolder)) 
{ 
    File.Delete(filePath); 
} 

を行うことができ、ディレクトリ内のすべてのファイルを削除するために

enter image description here

Source

は、あなたがしたい場合は時にスローされますディレクトリ全体、その中のすべてのファイルとサブフォルダを削除する任意の例外なく、私のため

Directory.Delete(recentDocumentsFolder, true); 
2

あなたのコードの作業を呼び出すことができ、私はこの方法を用いた最近のドキュメントフォルダを選択して、完璧なここ

System.Environment.GetFolderPath(Environment.SpecialFolder.Recent) 

は私のテストソリューションは、コンソールアプリケーション

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml; 
using System.Xml.Linq; 
using Newtonsoft.Json; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string rd = System.Environment.GetFolderPath(Environment.SpecialFolder.Recent); 
      DeleteRecentDocuments(rd); 

      Console.ReadLine(); 
     } 

     //this will delete the the files in the Recent Documents directory 
     private static void DeleteRecentDocuments(string RecentDocumentsDirectory) 
     { 
      //this is the directory and parameter which we will pass when we call the method 
      DirectoryInfo cleanRecentDocuments = new DirectoryInfo(RecentDocumentsDirectory); 

      //try this code 
      try 
      { 
       //loop through the directory we use the getFiles method to collect all files which is stored in recentDocumentsFolder variable 
       foreach (FileInfo recentDocumentsFolder in cleanRecentDocuments.GetFiles()) 
       { 
        //we delete all files in that directory 
        recentDocumentsFolder.Delete(); 
       } 
      } 
      //catch any possible error and display a message 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 

} 
使用しているが動作してい

更新

そのディレクトリ内のいくつかのファイルがないだけで保護され、削除のためだけでなく、あなたがそれらを削除することはできませんが、他のほとんどはコードの下に使用して削除することができますので、私はこれがあなた

+0

助けてくれてありがとう最近のドキュメントフォルダのファイルを削除したいだけです。 – DialUp

+0

これは私がメソッドと呼ぶものです:if(ThumbnailCacheBox.Checked == true) { CleanThumbnailCache(@ "C:\ Users \" + System.Environment.UserName + @ "\ AppData \ Local \ Microsoft \ Windows \ Explorer "); } – DialUp

+0

私の更新を見てください、いくつかのファイルは削除できませんが、他のものが可能です – Mostafiz

関連する問題