Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:52:50+00:00 2026-05-28T05:52:50+00:00

How to bypass/ignore Access to the path is denied/UnauthorizedAccess exception and continue to collecting

  • 0

How to bypass/ignore “Access to the path is denied”/UnauthorizedAccess exception

and continue to collecting filenames in this method;

public static string[] GetFilesAndFoldersCMethod(string path)
{
   string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray();
   return filenames;
}

//Calling… …

foreach (var s in GetFilesAndFoldersCMethod(@"C:/"))
{
    Console.WriteLine(s);
}

My application stops on the firstline of GetFilesAndFoldersCMethod and an exception says;
“Access to the path ‘C:\@Logs\’ is denied.”. Please help me…

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:52:51+00:00Added an answer on May 28, 2026 at 5:52 am

    Best way to do this is to add a Try/Catch block to handle the exception…

    try
    {
       string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray();
       return filenames;
    }
    catch (Exception ex)
    {
       //Do something when you dont have access
       return null;//if you return null remember to handle it in calling code
    }
    

    you can also specifically handle the UnauthorizedAccessException if you are doing other code in this function and you want to make sure it is an access exception that causes it to fail (this exception is thrown by the Directory.GetFiles function)…

    try
    {
       //...
    }
    catch(UnauthorizedAccessException ex)
    {
        //User cannot access directory
    }
    catch(Exception ex)
    {
        //a different exception
    }
    

    EDIT: As pointed out in the comments below it appears you are doing a recursive search with the GetFiles function call. If you want this to bypass any errors and carry on then you will need to write your own recursive function. There is a great example here that will do what you need. Here is a modification which should be exactly what you need…

    List<string> DirSearch(string sDir) 
    {
       List<string> files = new List<string>();
    
       try  
       {
          foreach (string f in Directory.GetFiles(sDir)) 
          {
             files.Add(f);
          }
    
          foreach (string d in Directory.GetDirectories(sDir)) 
          {
             files.AddRange(DirSearch(d));
          }
       }
       catch (System.Exception excpt) 
       {
          Console.WriteLine(excpt.Message);
       }
    
       return files;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this article I don't have a clear answer: http://palizine.plynt.com/issues/2010Oct/bypass-xss-filters/ Will browsers interpret
I am trying to bypass the call JNIEnv->DefineClass method with all necessary parameter but
I'd like to bypass the Do you want to continue (y/n, default n) prompt
Is there a way to bypass the exception as named in the question title?
Ok so this is my first applet, I tried many times to bypass this
Testing this jquery code on external sites (loaded via proxy to bypass Single Origin
I want Doxygen to ignore, bypass, not search the following directories of my project:
I have troubles to call this method: myXmlHttpRequest.send(data); It's because, the Webservice is HTTPS!
Ho can i bypass this restriction? I know i can use some kind of
I wanted to bypass an internal method call and hence have mocked it. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.