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. Unusual behaviour with Directory.Exists() method

Unusual behaviour with Directory.Exists() method

Scheduled Pinned Locked Moved C#
helptestingbeta-testingquestion
2 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.
  • C Offline
    C Offline
    Cracked Down
    wrote on last edited by
    #1

    Hey all, I came across one unusual behavior of Directory.Exists() method when I was testing user path input, I was trying with different inputs just to test the existence of the directory on my local drive. For negative test cases I provided the existing directory path and put two ".." at the end of directory path e.g. lets say I have "C:\CurrentDir" on my local drive when I check the existence of directory using bool isDirExist = Directory.Exists(@"C:\CurrentDir..");//".." are appended surprisingly isDirExist is true :omg: when I try to access file contained in this directory it throws error......:mad: it does not throw exception if I append only one "." [for @"C:\CurrentDir." +AnyFileName] :wtf: Is this is bug in Directory.Exists method or anything else is happening behind the screen at OS level??

    N 1 Reply Last reply
    0
    • C Cracked Down

      Hey all, I came across one unusual behavior of Directory.Exists() method when I was testing user path input, I was trying with different inputs just to test the existence of the directory on my local drive. For negative test cases I provided the existing directory path and put two ".." at the end of directory path e.g. lets say I have "C:\CurrentDir" on my local drive when I check the existence of directory using bool isDirExist = Directory.Exists(@"C:\CurrentDir..");//".." are appended surprisingly isDirExist is true :omg: when I try to access file contained in this directory it throws error......:mad: it does not throw exception if I append only one "." [for @"C:\CurrentDir." +AnyFileName] :wtf: Is this is bug in Directory.Exists method or anything else is happening behind the screen at OS level??

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      SaveTigers wrote:

      surprisingly isDirExist is true

      Directory.Exists is working as expected. You can't create a directory that ends with . (DOT) in windows. Windows will remove the dots automatically. So the function is not caring about the dots. You can do something like.

      string directoryToCheck = Path.GetFullPath(@"C:\CurrentDir.."); // This will trim the dots at the end
      bool isDirExist = Directory.Exists(directoryToCheck); // Will work as expected

      :)

      Best wishes, Navaneeth

      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