Every time I run the code below, I get an UnauthorizedAccessException
. I added try
and catch
blocks, which prevented the error, but it stopped the program dead in it's tracks.
Is there a way I can ignore this error and read the Unauthorized Access files? If not, I would just like my program to skip these files continue without stopping.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace Interface
{
class Progra
{
static void Main(string[] args)
{
try
{
int number = 0;
string[] files = Directory.GetFiles("C:\\", "*.*",
SearchOption.AllDirectories);
foreach (string file in files)
{
number = number + 1;
Console.ForegroundColor = ConsoleColor.Green;
DateTime now = DateTime.Now;
Console.Write("[" + now + "]");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(" [" + number + "] ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(file + "\n");
}
Console.WriteLine("");
Console.WriteLine("- " + number + " files found!");
Console.ReadKey();
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("- An unknown error occoured and the contents " +
"of this folder can not be displayed.\n");
Console.ForegroundColor = ConsoleColor.White;
}
Console.ReadKey();
}
}
}
If there is an answer, can you show me how I can do it? Noob at C# :P .