Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
public Boolean CleanRecentData()
{
        isAllClean = true;
        String SysRecentPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Recent";
        DirectoryInfo SysRecDir = new DirectoryInfo(SysRecentPath);
        File.SetAttributes(SysRecentPath, FileAttributes.Normal);

foreach (FileInfo fi in SysRecDir.GetFiles())   //Access Denied
                                                 //Exception is thrown here
        {
            try
            {
                fi.Delete();
            }
            catch (Exception ex)
            {
                recentLogLines.AppendLine(ex.Message);
                isAllClean = false;
            }
        }

        foreach (DirectoryInfo dir in SysRecDir.GetDirectories())
        {
            try
            {
                dir.Delete(true);
            }
            catch (Exception ex)
            {
                recentLogLines.AppendLine(ex.Message);
                isAllClean = false;
            }
        }

        return isAllClean;
    }
Posted
Updated 4-Dec-14 20:07pm
v2
Comments
Thanks7872 5-Dec-14 0:54am    
Its not necessary to post different questions on same topic. One for fetching files, another for deleting files. Follow single thread in such case.

1 solution

You have to run your application as Administrator in order to have access rights to these files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900