简体   繁体   English

C#未经授权的异常

[英]c# Unauthorized exception

Hey guys I got this error I have tried to run program as administrator, no luck still get this error I don't get why it can't clean the shortcuts in the recent documents folder, this is my code: 嗨,我遇到了这个错误,我试图以管理员身份运行程序,但还是没有运气得到此错误,我不明白为什么它不能清除最近文档文件夹中的快捷方式,这是我的代码:

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

I call this method above but dw bout that too much its just calling the method and parameter is the directory. 我在上面调用了此方法,但是dw认为它仅调用方法和参数就是目录。 If you want I can post that to. 如果您愿意,我可以将其发布到。

According to MSDN, FileInfo.Delete() will throw UnauthorizedAccessException when 根据MSDN, FileInfo.Delete()会在以下情况时抛出UnauthorizedAccessException

在此处输入图片说明

Source 资源

In order to delete all the files in a directory could do 为了删除目录中的所有文件可以做

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

If you want to delete the entire directory and any files and subfolders within it you could call 如果要删除整个目录以及其中的所有文件和子文件夹,可以调用

Directory.Delete(recentDocumentsFolder, true);

Your code work for me without any exception, I have select recent document folder using this way and work perfect 您的代码毫无例外地为我工作,我已经使用这种方式选择了最近的文档文件夹,并且工作完美

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

here is my test solution using console application 这是我使用控制台应用程序的测试解决方案

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

}

Update 更新资料

some files inside that directory is protected not only for delete but also copy so you can't delete them but most of others can be delete using below code, I have tested 我测试过,该目录中的某些文件不仅受删除保护,还受复制保护,因此您无法删除它们,但其他大多数文件都可以使用以下代码删除

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

Hope this will help you 希望这个能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM