Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need Help

TeejayTeejay South AfricaPosts: 2Member
in C#

I've been trying to list all files located on my main drive in a console application but I keep getting the UnauthorizedAccessException.

Here is my code: `using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;

namespace ConsoleApplication5 { class Program {

    static void Main(string[] args)
    {
            try
            {
                foreach(string file in Directory.GetFiles(@"C:/", "*", SearchOption.AllDirectories))
                {
                    Console.WriteLine(file);
                }
            }
        catch (UnauthorizedAccessException ex)
            {
            Console.WriteLine(ex.Message);
            }
        Console.ReadLine();
    }
}

}`

Here is the exception message: Access to path 'C:\$Recycle.Bin\S-1-5-18' is denied.

I have tried to avoid to exception like so:

if (!file.Contains("RECYLCE") && !file.Contains("Recycle"))
{
    Console.WriteLine(file);
}
But unfortunately it did not work.

Help would be appreciated. I need a way to skip the exception and carry on listing the rest of the files.

Tagged:
Sign In or Register to comment.