Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. skipping access denied folders when using GetDirectories()

skipping access denied folders when using GetDirectories()

Scheduled Pinned Locked Moved C#
question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lane0p2
    wrote on last edited by
    #1

    Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil

    B L D 3 Replies Last reply
    0
    • L lane0p2

      Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #2

      use a try/catch?

      Help me! I'm turning into a grapefruit! Buzzwords!

      1 Reply Last reply
      0
      • L lane0p2

        Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        use try-catch inside the loop. That is what I do. BTW: .NET 4.0 will offer better methods to enumerate files/folders, without requiring a (possibly huge) array, so one will be able to really use the recursive mode. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        D 1 Reply Last reply
        0
        • L lane0p2

          Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil

          D Online
          D Online
          dan sh
          wrote on last edited by
          #4

          AFAIK there is no such option present. You will have to write your own method to do this.

          L 1 Reply Last reply
          0
          • L Luc Pattyn

            use try-catch inside the loop. That is what I do. BTW: .NET 4.0 will offer better methods to enumerate files/folders, without requiring a (possibly huge) array, so one will be able to really use the recursive mode. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            D Online
            D Online
            dan sh
            wrote on last edited by
            #5

            That will not work. The exception will occur while executing the GetDirectories method itself and control will never go inside the try/catch.

            L 1 Reply Last reply
            0
            • D dan sh

              That will not work. The exception will occur while executing the GetDirectories method itself and control will never go inside the try/catch.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              :confused: It works for me, I am listing all files/folders of any device I choose (both XP and Vista). If GetDirectories() and GetFiles() would not cope with inaccessible files and folders, they would be completely useless, wouldn't they? :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              D 1 Reply Last reply
              0
              • L Luc Pattyn

                :confused: It works for me, I am listing all files/folders of any device I choose (both XP and Vista). If GetDirectories() and GetFiles() would not cope with inaccessible files and folders, they would be completely useless, wouldn't they? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                D Online
                D Online
                dan sh
                wrote on last edited by
                #7

                In XP SP2, I created a folder(c:\TestFolder) and "denied" all the permissions under security tab. GetDirectories method itself will throw exception. Control never came to try/catch. See my code:

                  DirectoryInfo dirInfo = new DirectoryInfo(@"C:\\");
                  foreach (DirectoryInfo dir in dirInfo.GetDirectories("\*.\*", SearchOption.AllDirectories)) {
                    try {
                    }
                    catch {
                    }
                  }
                
                L 1 Reply Last reply
                0
                • D dan sh

                  In XP SP2, I created a folder(c:\TestFolder) and "denied" all the permissions under security tab. GetDirectories method itself will throw exception. Control never came to try/catch. See my code:

                    DirectoryInfo dirInfo = new DirectoryInfo(@"C:\\");
                    foreach (DirectoryInfo dir in dirInfo.GetDirectories("\*.\*", SearchOption.AllDirectories)) {
                      try {
                      }
                      catch {
                      }
                    }
                  
                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  That is new to me. I'll have to investigate. And what is your solution? :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  D 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    That is new to me. I'll have to investigate. And what is your solution? :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    D Online
                    D Online
                    dan sh
                    wrote on last edited by
                    #9

                    Never faced this issue, so far. If I do, I would go for Linq. May be something along the lines of this[^] approach. If it .Net 2.0, Linq bridge[^] might save me.

                    1 Reply Last reply
                    0
                    • D dan sh

                      AFAIK there is no such option present. You will have to write your own method to do this.

                      L Offline
                      L Offline
                      lane0p2
                      wrote on last edited by
                      #10

                      Cool, thanks guy, i thought there would be an easy option!!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups