COMException When Trying to Access Any Member For an IIS DirectoryEntry

I've been playing around with writing a Custom Installer Actions for a VS2005 Web Application Deployment Project (aka Web Application Installer).  The most challenging thing after figuring out the syntax weirdnesses for Custom Installers was manipulating IIS from C#. 

There are a ton of blog entries out there on how to manipulate IIS from .NET but I was still having problems getting the code to work.  The big error that I kept getting was

System.Runtime.InteropServices.COMException: The system cannot find the path specified.

This error showed up whenever I tried to access any property or method on the DirectoryEntry object for the IIS directory. The error even showed up when I was in the debugger. 

The solution came from Saar Carmi's blog post.  All the other posts out there got it MOSTLY right but Saar's got this one little extra line in there:

folderRoot.RefreshCache();

That's it.  That's the line that makes EVERYTHING work.  Call RefreshCache() on the DirectoryEntry after you've created it and suddenly all those properties work.  (Thanks, Saar!)

Here's a working piece of code piece of code:

public static DirectoryEntry FindDirectoryForPath(string appDirectory)
{
	string machineName = System.Environment.MachineName;
	string query;

	query = String.Format("IIS://{0}/w3svc", machineName);

	DirectoryEntry w3svc = new DirectoryEntry(query);

	DirectoryEntry appDirectoryEntry = null;

	foreach (DirectoryEntry webSite in w3svc.Children)
	{
		query = String.Format(
			"IIS://{0}/W3SVC/{1}/ROOT/{2}",
			machineName, webSite.Name, appDirectory);

		try
		{
			appDirectoryEntry = new DirectoryEntry(query);

			if (appDirectoryEntry != null)
			{
				appDirectoryEntry.RefreshCache();
				return appDirectoryEntry;
			}
		}
		catch (COMException)
		{

		}
		catch (Exception)
		{
			// not this one
		}
	}

	// couldn't find anything
	return null;
}

-Ben

posted @ Tuesday, January 02, 2007 6:37 AM

Print

Comments on this entry:

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by turkey at 7/30/2008 1:21 PM
Gravatar
thanx code error line 5

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by rüya tabiri at 11/25/2008 2:34 AM
Gravatar
Thank you...

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by flas game at 12/2/2008 9:32 AM
Gravatar
thanks

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by chat at 1/3/2009 2:34 PM
Gravatar
thank you :)

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by rüya tabirleri at 1/8/2009 6:29 PM
Gravatar
Thank you..

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by sohbet odalari at 3/7/2009 3:47 AM
Gravatar
thank you !

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Sohbet at 3/7/2009 4:41 PM
Gravatar
thanks you

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Sohbet Odalari at 3/7/2009 4:42 PM
Gravatar
thanksss youuu

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Mirc at 3/7/2009 4:43 PM
Gravatar
thnkssssssssssssssssss

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Chat at 3/13/2009 5:17 AM
Gravatar
thank you cano

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Ligtv izle at 3/19/2009 2:00 AM
Gravatar
thank you kankamm.

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Mynet at 3/23/2009 8:02 AM
Gravatar
thank you kanka

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by mirc yükle at 4/3/2009 10:39 AM
Gravatar
thank you babili

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by ligtv izle at 4/18/2009 1:39 PM
Gravatar
thank youm

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by chat odaları at 4/28/2009 4:49 AM
Gravatar
tankix

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Sohbet Chat Sevgi at 5/8/2009 6:58 PM
Gravatar
çok çok mersi.....

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Sohbet Chat Sevgi Ask at 5/8/2009 6:59 PM
Gravatar
sağolasın kardess..

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Sohbet at 5/10/2009 1:40 PM
Gravatar
hallo i wish you succes on your job thanks a lot

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Sohbet at 5/12/2009 7:16 PM
Gravatar
hallo i wish you verry succes operator

# re: COMException When Trying to Access Any Member For an IIS DirectoryEntry

Left by Edencity Chat at 5/22/2009 8:37 PM
Gravatar
hallo i wish you verry succes operator

Your comment:



 (will not be displayed)


 
 
 
Please add 2 and 3 and type the answer here:
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678