-1

I've tried

if(Directory.Exists(path)) 
    //is directory
else
    //is file

but this doesn't always work. not sure why. Any ideas?

Rob
  • 26,989
  • 16
  • 82
  • 98
Ben
  • 5,627
  • 9
  • 35
  • 49

1 Answers1

2

Both Directory.Exists and File.Exists will return false if an error occurs during the check, or if the user does not have sufficient permissions.

From the docs at Directory.Exists

If you do not have at a minimum read-only permission to the directory, the Exists method will return false. The Exists method returns false if any error occurs while trying to determine if the specified file exists.

From the docs at File.Exists

If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path... The Exists method returns false if any error occurs while trying to determine if the specified file exists.

Seth Flowers
  • 8,990
  • 2
  • 29
  • 42