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?