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. Other Discussions
  3. Clever Code
  4. Directory.GetFiles oddity.

Directory.GetFiles oddity.

Scheduled Pinned Locked Moved Clever Code
helpdebugginglounge
12 Posts 9 Posters 1 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.
  • D Offline
    D Offline
    Dan Neely
    wrote on last edited by
    #1

    Something I discovered today, while in general Directory.GetFiles() doesn't need a terminating slash in the path in the specific case of the root directory on a drive it does. "C:\blah" works fine; "C:\" works fine "C:" returns zero results even if the files exist. I suppose this could be argued as a gray area but it doesn't appear to be documented in MSDN, and if it's being considered an illegal path it should throw an exception instead of failing silently. It was easy to find once I fired up the debugger, but I'd've never expected it to be a problem until I got the bug report...

    Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

    P P J 4 Replies Last reply
    0
    • D Dan Neely

      Something I discovered today, while in general Directory.GetFiles() doesn't need a terminating slash in the path in the specific case of the root directory on a drive it does. "C:\blah" works fine; "C:\" works fine "C:" returns zero results even if the files exist. I suppose this could be argued as a gray area but it doesn't appear to be documented in MSDN, and if it's being considered an illegal path it should throw an exception instead of failing silently. It was easy to find once I fired up the debugger, but I'd've never expected it to be a problem until I got the bug report...

      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

      P Offline
      P Offline
      Phil J Pearson
      wrote on last edited by
      #2

      I agree with you that it shouldn't behave that way but there is a certain amount of logic to it: C: is a DRIVE C:\ is a FOLDER C:\blah may be a FOLDER or a FILE but it can tell by looking C:\blah\ is expected to refer to a FOLDER

      Phil


      The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

      D 1 Reply Last reply
      0
      • D Dan Neely

        Something I discovered today, while in general Directory.GetFiles() doesn't need a terminating slash in the path in the specific case of the root directory on a drive it does. "C:\blah" works fine; "C:\" works fine "C:" returns zero results even if the files exist. I suppose this could be argued as a gray area but it doesn't appear to be documented in MSDN, and if it's being considered an illegal path it should throw an exception instead of failing silently. It was easy to find once I fired up the debugger, but I'd've never expected it to be a problem until I got the bug report...

        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        DOS-style file specifications suc... ceed at causing all sorts of trouble. (And don't get me started on file names containing SPACEs :mad: ) OpenVMS rules!!

        1 Reply Last reply
        0
        • P Phil J Pearson

          I agree with you that it shouldn't behave that way but there is a certain amount of logic to it: C: is a DRIVE C:\ is a FOLDER C:\blah may be a FOLDER or a FILE but it can tell by looking C:\blah\ is expected to refer to a FOLDER

          Phil


          The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          If it threw a NotADirectorException I'd be at most mildly annoyed with the implementation, the silent failure is the real problem.

          Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

          L P 2 Replies Last reply
          0
          • D Dan Neely

            If it threw a NotADirectorException I'd be at most mildly annoyed with the implementation, the silent failure is the real problem.

            Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            dan neely wrote:

            NotADirectorException

            CEO centric-code? ;P

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            E 1 Reply Last reply
            0
            • D Dan Neely

              If it threw a NotADirectorException I'd be at most mildly annoyed with the implementation, the silent failure is the real problem.

              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              dan neely wrote:

              the silent failure is the real problem.

              That's the bad part. You'd think someone would just have it throw an exception so you could at least do something when that happens.

              "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

              1 Reply Last reply
              0
              • D Dan Neely

                Something I discovered today, while in general Directory.GetFiles() doesn't need a terminating slash in the path in the specific case of the root directory on a drive it does. "C:\blah" works fine; "C:\" works fine "C:" returns zero results even if the files exist. I suppose this could be argued as a gray area but it doesn't appear to be documented in MSDN, and if it's being considered an illegal path it should throw an exception instead of failing silently. It was easy to find once I fired up the debugger, but I'd've never expected it to be a problem until I got the bug report...

                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                J Offline
                J Offline
                jbarton
                wrote on last edited by
                #7

                The result of Directory.GetFiles("c:") is well defined. You get the same result as if you typed "dir c:" from a command prompt. The result is the contents of the current directory on the c drive. The result of Directory.GetFiles(@"c:\") is the contents of the root directory. If you leave off the '\', Directory.GetFiles() will return a relative path to the current directory on the specified drive. So, Directory.GetFiles(@"c:\blah") is the contents of the blah directory off of the root and Directory.GetFiles(@"c:blah") is the contents of the blah directory off of the current directory on the c drive. If you leave off the drive, Directory.GetFiles() will return either an absolute or relative path on the current drive. So, Directory.GetFiles(@"\blah") is the contents of the blah directory on the current drive and Directory.GetFiles(@"blah") is the contents of the blah directory off of the current directory of the current drive.

                D 1 Reply Last reply
                0
                • D Dan Neely

                  Something I discovered today, while in general Directory.GetFiles() doesn't need a terminating slash in the path in the specific case of the root directory on a drive it does. "C:\blah" works fine; "C:\" works fine "C:" returns zero results even if the files exist. I suppose this could be argued as a gray area but it doesn't appear to be documented in MSDN, and if it's being considered an illegal path it should throw an exception instead of failing silently. It was easy to find once I fired up the debugger, but I'd've never expected it to be a problem until I got the bug report...

                  Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Doesn't it work like the DIR command? If you don't specify the directory it assumes the last one you accessed* on that drive? * That's not worded clearly, but I hope the concept gets across.

                  1 Reply Last reply
                  0
                  • J jbarton

                    The result of Directory.GetFiles("c:") is well defined. You get the same result as if you typed "dir c:" from a command prompt. The result is the contents of the current directory on the c drive. The result of Directory.GetFiles(@"c:\") is the contents of the root directory. If you leave off the '\', Directory.GetFiles() will return a relative path to the current directory on the specified drive. So, Directory.GetFiles(@"c:\blah") is the contents of the blah directory off of the root and Directory.GetFiles(@"c:blah") is the contents of the blah directory off of the current directory on the c drive. If you leave off the drive, Directory.GetFiles() will return either an absolute or relative path on the current drive. So, Directory.GetFiles(@"\blah") is the contents of the blah directory on the current drive and Directory.GetFiles(@"blah") is the contents of the blah directory off of the current directory of the current drive.

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #9

                    ahh. I was using the overload with a filter and assuming the default directory is the one where the executable was located nothing would be returned. Obscure legacy support. *sigh*

                    Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                    H P 2 Replies Last reply
                    0
                    • D Dan Neely

                      ahh. I was using the overload with a filter and assuming the default directory is the one where the executable was located nothing would be returned. Obscure legacy support. *sigh*

                      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                      H Offline
                      H Offline
                      HosamAly
                      wrote on last edited by
                      #10

                      dan neely wrote:

                      assuming the default directory is the one where the executable was located

                      The default directory is not necessarily where the executable was located. If you run it through a shortcut, it's the "Start in:" directory. If you run it from CMD, it's the current directory when the application was called. If it is being run from another process, it's the default folder of that process.

                      1 Reply Last reply
                      0
                      • D Dan Neely

                        ahh. I was using the overload with a filter and assuming the default directory is the one where the executable was located nothing would be returned. Obscure legacy support. *sigh*

                        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                        P Offline
                        P Offline
                        peterchen
                        wrote on last edited by
                        #11

                        dan neely wrote:

                        Obscure legacy support

                        Hey, the league of old fart keyboard jockeys would liek to have a talk with you! (I relying on this feature very often on the console)

                        Burning Chrome ^ | Linkify!| FoldWithUs! | sighist

                        1 Reply Last reply
                        0
                        • L leppie

                          dan neely wrote:

                          NotADirectorException

                          CEO centric-code? ;P

                          xacc.ide - now with TabsToSpaces support
                          IronScheme - 1.0 alpha 4a out now (29 May 2008)

                          E Offline
                          E Offline
                          E del Ayre
                          wrote on last edited by
                          #12

                          :laugh:

                          think fast, be brave and dont stop.

                          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