none
c# - get sub directory/sub file names using linq and skip access denied folders/sub folders etc. RRS feed

  • Question

  • how to get sub directory/sub file names using linq and skip access denied folders/sub folders etc.
    if the folder is access denied, I get this error

    Attempted to perform an unauthorized operation.

    so I want to skip every folder subfolder and files if it is access denied

    I tried this one

          var dir =
            from i in System.IO.Directory.GetDirectories(@"D:\")
            where new System.IO.DirectoryInfo(i).GetAccessControl(System.Security.AccessControl.AccessControlSections.Owner).Equals(AccessControlActions.None)
            select new{filename = new System.IO.FileInfo(i).FullName};

    still give me the same error.

    Thursday, May 29, 2014 7:21 AM

Answers

  • Obtaining the access control list of a directory is itself subject to access control checks, if you do not have permissions to that directory you'll get an exception too.

    The proper way to do this is to do the operation you intend to do on those directories and handle the appropriate exceptions that may be thrown. And you probably can forget about LINQ in this case, it's not like it solves any problem.

    Thursday, May 29, 2014 8:33 AM
    Moderator