IIS - creating new web site throws exception

2

Running the following code:

DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
int siteId = GetUniqueSiteId(Path);
private string DEFAULT_WEB_SITE_PHYSICAL_PATH = System.IO.Path.GetPathRoot(Environment.SystemDirectory) + @"inetpub\wwwroot";

object[] newsite = new object[] { "Default Web Site", new object[] { ":80:" }, DEFAULT_WEB_SITE_PHYSICAL_PATH, siteId };

w3svc.Invoke("CreateNewSite", newsite);

throws the following exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x800700B7): Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)

I'm trying to run this code only when "Default Web Site" doesn't exists. The exception is raised only when running in Windows Server 2003. Moreover, when running this code in a loop that retries and sleeps for 1sec after every failure, that code finally works after 3 reties and the default website is created.

Anyone knows what might be the cause?

c#
iis
windows-2003-webserver
asked on Stack Overflow Oct 18, 2011 by galbarm • edited Oct 18, 2011 by Steve B

1 Answer

0

You are trying to create "Default Web Site" when "Default Web Site" exists. You said yourself it works fine when the website does not exist....

answered on Stack Overflow Oct 18, 2011 by ChrisBint

User contributions licensed under CC BY-SA 3.0