.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Directory.Exists() incorrect with UNC paths
 

Directory.Exists() incorrect with UNC paths

Hi,

I'm experiencing some strange behaviour with Directory.Exists and UNC paths.

Firstly if I check the root of a UNC path (e.g. \\servername) it always returns false whether the server exists or not, although it takes far longer if the server name is invalid. I can kind of accept this as it can be argued that this is not a folder. However I'd like to know how to check a root UNC path is valid? Is Ping a good way?

My real problem occurs when I check a longer path with a valid server name (e.g. \\validservername\someinvalidpath). This always returns true if the second part of the path is invalid. So:

\\validservername\validpathpart = true
\\validservername\validpathpart\validpathpart = true
\\validservername\validpathpart\invalidpathpart = false

which is correct, but:

\\validservername\invalidpathpart = true
\\validservername\invalidpathpart\anypathpart = true

which is not!


Strangely if I type an invalid path into windows explorer, it opens as if the folder does exist although it says "This folder is empty" and then Explorer hangs if I try to create a file in my non-existent directory. I wonder if there's something strange with our network set-up?

Either way this leaves me with no method of accurately checking the validity of a network path.

I tried some of the other Directory methods on my invalid UNC path and Directory.GetCreationTime returns 01/01/1601 but doesn't fail. Directory.GetAccessControl fails with an InvalidOperationException and the (really helpful) message "Method failed with unexpected error code 1.".

So I was thinking of using GetAccessControl to double check the path, but apart from being expensive I'm not sure if this is a valid thing to do.

Any help appreciated ....

LJ



Laughing John
bump!
Laughing John
Hi,

i have just tested this with the windwos explorer on my Win XP machine with VB 2005 and i got the error message that the file \\validserver\invalidpath does not exist and i should check the typing.

also tried Directory.Exists with the expected result:

\\validservername\validpathpart = true
\\validservername\validpathpart\validpathpart = true
\\validservername\validpathpart\invalidpathpart = false


If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Heslacher
Heslacher,

Thanks for trying it out, that's interesting.

I tried it both on an XP and Vista machine here and the results are the same. I guess it must be something to do with our network setup although this is of concern as we are a software house so we have to take account of different clients and it's not impossible someone else has the same setup as us.

It only seems to be wrong if the second part of the path is invalid, in which case directory.exists() returns true and in Explorer (both in Vista and XP) it displays an empty folder instead of an error message. Maybe it's something to do with the second part of the path being the share name.

I guess I will have to find a network admin and see if they can explain what's going on.

LJ.
Laughing John

You can use google to search for other answers

Custom Search

More Threads

• SerialPort class in non-overlapped mode
• Random number generator
• Background Worker and database errors
• Joining Strings together in a colum from a text file to one row
• NullReference Error
• Fixed-size Queue?
• Required Date Range Validation
• How to determine which form called a class
• Activate timer from other thread
• serial communication