C
C#3y ago
Meistro

System.UnauthorizedAccessException [Answered]

Hei I need help with this line
foreach (string valid_path in Directory.EnumerateDirectories(dir, validFolderName, SearchOption.AllDirectories))
foreach (string valid_path in Directory.EnumerateDirectories(dir, validFolderName, SearchOption.AllDirectories))
I want to search for a specific Folder in my Drive "C:\" But it always give me access denie for C:$Recycle.Bin. I want to skip it but it doesnt work even with try cath. Thanks for any help
9 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Meistro
MeistroOP3y ago
oh sorry I thought you are part of the team
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Meistro
MeistroOP3y ago
ok I will try
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Meistro
MeistroOP3y ago
ok I will remove now The admin thing is now working
Meistro
MeistroOP3y ago
Well I found this on the internet (Stuckoverflow): Here's Best and Fastest Way To List All Directories and Subdirectories by Skipping System.UnauthorizedAccessException A simple recursive method with action to retrieve at realtime, preparing a list and returning to parent takes a lot of time, this call back function helps a lot to retrieve at realtime instead of preparing and returning back
using System.IO;

private static void ListDirectories(string Path, Action<string> Listen){
try
{
foreach(string Node in Directory.GetDirectories(Path)){
Listen(Node);
ListDirectories(Node, Listen);
}
} catch {}
}
using System.IO;

private static void ListDirectories(string Path, Action<string> Listen){
try
{
foreach(string Node in Directory.GetDirectories(Path)){
Listen(Node);
ListDirectories(Node, Listen);
}
} catch {}
}
Usase
static void Main(string[] args)
{
ListDirectories(@"C:\", (dir) =>
{
Console.WriteLine(dir);
});
}
static void Main(string[] args)
{
ListDirectories(@"C:\", (dir) =>
{
Console.WriteLine(dir);
});
}
source: https://stackoverflow.com/a/73528233
Stack Overflow
how to list all sub directories in a directory
I'm working on a project and I need to list all sub directories in a directory for example how could I list all the sub directories in c:\
Meistro
MeistroOP3y ago
this helped me and it works now thank you !close
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server