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