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 sohbet at 9/1/2009 10:29 PM
Gravatar
thanx you

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

Left by Sohbet at 10/17/2009 9:30 AM
Gravatar
thank you :) Askum

# 3d oyunlar

Left by ben 10 oyun at 10/24/2009 12:07 PM
Gravatar
thanks for all admin
süperrrr

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

Left by Lloyd L. at 1/8/2010 11:35 AM
Gravatar
It only works for me if the new directory is created at root (eg:IIS://localhost/W3SVC/1/ROOT), but not work if the new directory is under root folder (eg: IIS://localhost/W3SVC/1/ROOT/lloyd)

any suggestion? Thanks

Your comment:



 (will not be displayed)


 
 
 
Please add 2 and 5 and type the answer here:
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213