views:

48

answers:

2

Hello, I am faced with this strange problem:

In an .net web application, there is some code equivalent to this:

if (Directory.Exists("config")) 
{ ... }

It basically checks to see if C:\Windows\system32\inetsrv\config directory exists, why the code is written like this is beyond me and this question.

What confuses me is the above code would return false on two servers and return true on others. (I expected it to return true all the time, since C:\Windows\system32\inetsrv\config is a system directory in Server 2008.)

Question: I want to know why on those two servers this statement would return false. Where should I look?

Restriction: I cannot change any code (obfuscated/signed assemblies), but I have full access to the servers.

Update: The answer is apparently quite simple... on those two particular servers someone deployed a newer version of the particular dll which behaves differently. Damn!

+1  A: 

Well, the first thing I would check is that the directories in question actually exist on the server.

I assume you're passing in an absolute path - remember that Windows paths are not guaranteed to be the same on different machines. Localized versions of Windows, in particular, may mess with your assumptions about which directories exist and which don't.

Rather than referencing a directory with an explicit path:

Directory.Exists(@"C:\Windows\system32")

Use the provided special folder enumeration:

Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.System))

...though it seems unlikely that that part of the path would change on different machines.

Michael Petrotta
Thanks Michael, it is interesting insight, but unfortunately not related to the problem (it was a relative path "config" being passed in)
Bill Yang
+2  A: 

Take note of the following from the MSDN page

The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

I would start by double checking that the user running the web app (from the app pool identity, impersonation, etc) has permissions to the directory.

You say you expect it to true but have you gone ahead and verified the directory truly exists?

rchern
Ah, I had the assumption that if things failed it would throw exception, but after re-reading the MSDN page it does not seem to imply it. So I'll double check permissions and stuff. And to you question, yes the directory truly exists.
Bill Yang