user5417424
user5417424

Reputation:

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. If you want I can post that to.

Upvotes: 1

Views: 1223

Answers (2)

Mostafiz
Mostafiz

Reputation: 7352

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

Upvotes: 2

sparrow
sparrow

Reputation: 980

According to MSDN, FileInfo.Delete() will throw UnauthorizedAccessException when

enter image description here

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

Upvotes: 2

Related Questions