c#未经授权的例外

时间:2016-04-23 02:02:24

标签: 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回合说它只是调用方法和参数就是目录。如果你愿意,我可以将其发布到。

2 个答案:

答案 0 :(得分:2)

根据MSDN,FileInfo.Delete()会在

时抛出UnauthorizedAccessException

enter image description here

Source

为了删除目录中的所有文件,可以执行

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

如果要删除整个目录及其中的任何文件和子文件夹,可以调用

Directory.Delete(recentDocumentsFolder, true);

答案 1 :(得分: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);
            }
        }
    }

}

<强>更新

该目录中的一些文件不仅受到保护而且受到保护,因此您无法删除它们,但大多数其他文件可以使用下面的代码删除,我已经测试了

 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);
            }
        }

希望这会对你有所帮助