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

  • Home
  • SEARCH
  • 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 8294981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:25:30+00:00 2026-06-08T14:25:30+00:00

Possible Duplicate: .NET – Check if directory is accessible without exception handling Im making

  • 0

Possible Duplicate:
.NET – Check if directory is accessible without exception handling

Im making a small file explorer in Visual Studio 2010 with NET 3.5 and C#, and I have this function to check if a directory is accessible:

RealPath=@"c:\System Volume Information";
public bool IsAccessible()
{
    //get directory info
    DirectoryInfo realpath = new DirectoryInfo(RealPath);
    try
    {
        //if GetDirectories works then is accessible
        realpath.GetDirectories();                
        return true;
    }
    catch (Exception)
    {
        //if exception is not accesible
        return false;
    }
}

But I think with big directories it could be slow trying to get all sub directories to check if directory is accesible.
Im using this function to prevent errors when trying to explore protected folders or cd/dvd drives without disc (“Device Not Ready” error).

Is there a better way (faster) to check if directory is accessible by the application (preferably in NET 3.5)?

  • 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-06-08T14:25:32+00:00Added an answer on June 8, 2026 at 2:25 pm

    According to MSDN, Directory.Exists should return false if you don’t have read access to the directory. However, you can use Directory.GetAccessControl for this. Example:

    public static bool CanRead(string path)
    {
        try
        {
            var readAllow = false;
            var readDeny = false;
            var accessControlList = Directory.GetAccessControl(path);
            if(accessControlList == null)
                return false;
    
            //get the access rules that pertain to a valid SID/NTAccount.
            var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
            if(accessRules ==null)
               return false;
    
            //we want to go over these rules to ensure a valid SID has access
            foreach (FileSystemAccessRule rule in accessRules)
            {
                if ((FileSystemRights.Read & rule.FileSystemRights) != FileSystemRights.Read) continue;
    
                if (rule.AccessControlType == AccessControlType.Allow)
                    readAllow = true;
                else if (rule.AccessControlType == AccessControlType.Deny)
                    readDeny = true;
            }
    
            return readAllow && !readDeny;
        }
        catch(UnauthorizedAccessException ex)
        {
            return false;
        }
    }
    

    Update

    As mentioned in some comments, this may return an incorrect value in a case where a valid SID in an external DOMAIN has access. In order to check if the current user has access, you need something like:

    foreach...
    
    if (WindowsIdentity.GetCurrent().User.Value.equals(rule.IdentityReference.Value))
    

    This will confirm if the SID of the current user matches the access rule identity reference but may throw a SecurityException as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: .NET How to check if path is a file and not a
Possible Duplicate: Using ASP.NET Controls without databinding My previous question yielded few results so
Possible Duplicate: .Net Process.Start default directory? I have a C# application, mono to be
Possible Duplicate: ASP.NET postback with JavaScript I have a page without a form. But
Possible Duplicate: Running .net based application without .NET Framework Hello. Please let me know
Possible Duplicate: .NET file system wrapper library I would like to write a test
Possible Duplicate: .NET 3.5 chart controls exception: Error executing child request for ChartImg.axd I'm
Possible Duplicate: .Net Zip Up files I want to zip a .csv file using
Possible Duplicate: ASP.NET MVC Razor render without encoding One of the proerties of my
Possible Duplicate: ASP.NET MVC3 how to excute action method of controller using timer with

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.