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. .NET (Core and Framework)
  4. Best Practice Question When Enumerating Directories

Best Practice Question When Enumerating Directories

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharpdiscussioncareer
8 Posts 2 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.
  • A Offline
    A Offline
    Alan Burkhart
    wrote on last edited by
    #1

    I'm creating a "Find In Files" application for my own use in VB.net. After seeing all the problems people have working around UnauthorizedAccess exceptions, I used a windowless call to cmd.exe to get a directory list and sent it to a text file like so:

    sw.WriteLine("dir/s/b/ad >" & ChrW(34) & dataPath & ChrW(34))

    This avoids directories to which I don't have access, and it's pretty fast. But since I didn't see this anywhere else, I have to assume either I'm a genius or this is a really bad idea (probably the latter). I thought I might upload the project to CP when it's done, so my question is whether this is considered an acceptable practice or should I work on a different method of enumerating the directories?

    Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

    L 3 Replies Last reply
    0
    • A Alan Burkhart

      I'm creating a "Find In Files" application for my own use in VB.net. After seeing all the problems people have working around UnauthorizedAccess exceptions, I used a windowless call to cmd.exe to get a directory list and sent it to a text file like so:

      sw.WriteLine("dir/s/b/ad >" & ChrW(34) & dataPath & ChrW(34))

      This avoids directories to which I don't have access, and it's pretty fast. But since I didn't see this anywhere else, I have to assume either I'm a genius or this is a really bad idea (probably the latter). I thought I might upload the project to CP when it's done, so my question is whether this is considered an acceptable practice or should I work on a different method of enumerating the directories?

      Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      "Acceptable" to who? I would say there are more options than you may have considered; e.g. piping batch files; shell scripting; running app at admin level. There are very few "new" ways of doing things; only existing ways that can be improved upon.

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      1 Reply Last reply
      0
      • A Alan Burkhart

        I'm creating a "Find In Files" application for my own use in VB.net. After seeing all the problems people have working around UnauthorizedAccess exceptions, I used a windowless call to cmd.exe to get a directory list and sent it to a text file like so:

        sw.WriteLine("dir/s/b/ad >" & ChrW(34) & dataPath & ChrW(34))

        This avoids directories to which I don't have access, and it's pretty fast. But since I didn't see this anywhere else, I have to assume either I'm a genius or this is a really bad idea (probably the latter). I thought I might upload the project to CP when it's done, so my question is whether this is considered an acceptable practice or should I work on a different method of enumerating the directories?

        Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Why make things more complicated than they need to be? Use Directory.GetFiles Method (String) (System.IO)[^].

        A 1 Reply Last reply
        0
        • A Alan Burkhart

          I'm creating a "Find In Files" application for my own use in VB.net. After seeing all the problems people have working around UnauthorizedAccess exceptions, I used a windowless call to cmd.exe to get a directory list and sent it to a text file like so:

          sw.WriteLine("dir/s/b/ad >" & ChrW(34) & dataPath & ChrW(34))

          This avoids directories to which I don't have access, and it's pretty fast. But since I didn't see this anywhere else, I have to assume either I'm a genius or this is a really bad idea (probably the latter). I thought I might upload the project to CP when it's done, so my question is whether this is considered an acceptable practice or should I work on a different method of enumerating the directories?

          Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          If the Directory.GetFiles was slow for you with UnauthorizedAccessException's, then it is because there was a debugger attached.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          A 1 Reply Last reply
          0
          • L Lost User

            Why make things more complicated than they need to be? Use Directory.GetFiles Method (String) (System.IO)[^].

            A Offline
            A Offline
            Alan Burkhart
            wrote on last edited by
            #5

            Richard MacCutchan wrote:

            Why make things more complicated than they need to be?

            Because this allows UnauthorizedAccess exceptions to occur. As Eddy says below, this may be due to running in debug mode. Just now getting back to the project so I'll try running it outside the debugger. Also once the directory list is created I'm using Regex to search inside files, which GetFiles doesn't support. I'm learning as I go here. Thanks. :)

            Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

            L 1 Reply Last reply
            0
            • L Lost User

              If the Directory.GetFiles was slow for you with UnauthorizedAccessException's, then it is because there was a debugger attached.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              A Offline
              A Offline
              Alan Burkhart
              wrote on last edited by
              #6

              Just now getting back to the project. Will try running outside the debugger and see what happens. Thanks.

              Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

              1 Reply Last reply
              0
              • A Alan Burkhart

                Richard MacCutchan wrote:

                Why make things more complicated than they need to be?

                Because this allows UnauthorizedAccess exceptions to occur. As Eddy says below, this may be due to running in debug mode. Just now getting back to the project so I'll try running it outside the debugger. Also once the directory list is created I'm using Regex to search inside files, which GetFiles doesn't support. I'm learning as I go here. Thanks. :)

                Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You can also try one of Directory.EnumerateFileSystemEntries Method (System.IO)[^] which can list the contents of a single directory, and optionally, subdirectories.

                Alan Burkhart wrote:

                I'm using Regex to search inside files, which GetFiles doesn't support

                I don't understand what that means. Once you have a list of filenames you still need to open and read each one in order to search the contents.

                A 1 Reply Last reply
                0
                • L Lost User

                  You can also try one of Directory.EnumerateFileSystemEntries Method (System.IO)[^] which can list the contents of a single directory, and optionally, subdirectories.

                  Alan Burkhart wrote:

                  I'm using Regex to search inside files, which GetFiles doesn't support

                  I don't understand what that means. Once you have a list of filenames you still need to open and read each one in order to search the contents.

                  A Offline
                  A Offline
                  Alan Burkhart
                  wrote on last edited by
                  #8

                  Richard MacCutchan wrote:

                  I don't understand what that means. Once you have a list of filenames you still need to open and read each one in order to search the contents.

                  Just that Regex provides a few more options without slowing down the search.

                  Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

                  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