Using DirectoryInfo with a UNC path

wonea picture wonea · Apr 18, 2011 · Viewed 8.2k times · Source

I'm trying to DirectoryInfo to verify whether a UNC path exists, however DirectoryInfo consistency fails over and says the folder doesn't exist, when I can confirm it does in Windows Explorer.

Example

var storagepath = new DirectoryInfo(@"\\123.123.123.123\c$\testfolder\test1\");

if (storagepath.Exists)
{ }

Is there a way around this?

Answer

tamarintech picture tamarintech · Apr 18, 2011

There are a few questions that need to be answered in regards to your program.

Is it running in the same context as the user that you're verifying with explorer? Is it possible you are, for example, running a console application as a restricted user but logged in as a higher privilege user, etc?

One of the reasons for Exists being false includes not having permission to access the resource.

You could try another method like EnumerateDirectories and see what the result is. The exceptions and results of EnumerateDirectories are a bit more descriptive than Exists.