Saturday, February 11, 2006

Fix: COMException accessing DirectoryEntry.Properties["path"]

Earlier tonight, I was testing some C# 1.1 code that gets the physical path to an IIS virtual directory. I ran into a problem where for certain virtual directories on my test machine, a System.Runtime.InteropServices.COMException was thrown when trying to access the System.DirectoryServices.DirectoryEntry.Properties["path"] property for the directory's DirectoryEntry, whereas for other virtual directories, the code worked as expected.

After searching around using Google Groups briefly, I came across a microsoft.public.adsi.general post that ended up leading me to the answer. The post mentioned a property called "KeyType" of which I had been previously unaware.

Apparently, there are (at least) two types of IIS 5.1 virtual directories. The standard type has a DirectoryEntry.Properties["KeyType"] value of "IIsWebVirtualDir".

There is also a second type, which has a KeyType value of "IIsWebDirectory". Such virtual directories show up in inetmgr with the usual "virtual directory" icon (not the yellow "plain folder" icon) in the folder list in the right pane, but in the Properties dialog on the Directory tab, the Local Path field is disabled for such directories.

For "IIsWebDirectory" type directories, an attempt to access the DirectoryEntry.Properties["path"] property for the directory (to get the physical path to the directory) results in the System.Runtime.InteropServices.COMException ("Exception from HRESULT: 0x80005006") being thrown.

I ended up working around this issue by assuming any directories of type IIsWebDirectory are located under the root IIS virtual directory (for which DirectoryEntry.Properties["path"] does return a valid result). Unfortunately, I wasn't able to turn up any documentation on IIsWebDirectory-type directories to verify this assumption, but it seems reasonable given that the Local Path field in inetmgr for such directories is disabled.

No comments:

Post a Comment

Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!

Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!