0

So I see a lot of inquiries about this issue with files, not so many with directories - and the ones that involve directories, often have answers that are convoluted and all over the place.

I just want to basically know, if there is a good way to handle the problem of queries that hit directories that are inaccessible, due to inadequate permissions. It seems like a lot of the posts on this subject suggest recursion - but I'd love to avoid that, if possible.

    static string GetPathForMaskFiles(string startFolder)
    {
        // look for folders with a search string
        string[] dirs = Directory.GetDirectories(@"" + startFolder , "*posfiles*", SearchOption.AllDirectories);

        MessageBox.Show("There are " + dirs.Count() + " directories in the list");



        string oString = "";

        // placeholder for code when I get the query worked out

        return oString;
    }
Mhan7
  • 1
  • 3
  • No, there is no way to do that without handling exceptions on each attempt with recursion to walk the tree. – Alexei Levenkov Apr 04 '20 at 03:50
  • Even if I'm only trying to read the name of the directories? I'm not necessarily trying to open them. – Mhan7 Apr 04 '20 at 20:51
  • The code in the post uses "AllDirectories" to recursively search... If you just checking one level getting names of directories should be just fine. You can just look at [source](https://referencesource.microsoft.com/#mscorlib/system/io/directory.cs,44b0fd73d2306777) to confirm that there is no protection for AccessDenied on recursive version for yourself. – Alexei Levenkov Apr 04 '20 at 21:17
  • Well, my point is, I'm not trying to open directories. I'm just looking at their names, and want to scrape the full file path, when I find what I'm looking for. I mean, I get that I probably have to open some of them to do this, but I'm just grasping at straws here. Recursion is not what I want for what I'm doing. But if it's what I have to deal with... – Mhan7 Apr 04 '20 at 21:19
  • I don't understand what you hope for at all... sorry. If account that code runs under don't have permissions to enumerate content of a particular repository what do you expect to happen? Code can't get list of names inside... but there is no generally applicable way to handle that - maybe you don't need to list items in particular directory... So library function just re-throws access denied it got from OS - it's trivial to write recursive walk and write (copy-paste from duplicate) code that handles exceptions the way you need... – Alexei Levenkov Apr 04 '20 at 21:30
  • It's not about triviality, it's about runtime (which definitely won't be a trivial affair). I think my better approach might just be to find the registry key of the application, and start from there. – Mhan7 Apr 04 '20 at 22:07
  • What do you mean "about runtime"? Do you expect to write *significantly worse* recursion walk than one in .Net? (Indeed better way to ask the question is to ask what you wanted to do and show what you tried - it looks like you actually don't even need to search for file location... Also note that "list content" and "access files" are two *different* permissions - account may not be able to enumerate folders/files but launch/open file from such folder just fine) – Alexei Levenkov Apr 04 '20 at 22:16
  • To be honest, I asked the question according to my ability. In the process of trying to understand the issue better, I came across the notion of using registry key. I am a low level applications developer, so the nuance of much of this discussion escapes me. I deal with interop far more than I deal with data. But the intent is to build a solid method for finding a way to locate a directory that will survive a re-architecture of the software application that this pertains to. It's 3rd party, so beyond my control. Just trying to build a robust means to find the data that won't change. – Mhan7 Apr 04 '20 at 22:36

0 Answers0