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?

Steve B
  • 36,818
  • 21
  • 101
  • 174
galbarm
  • 2,441
  • 3
  • 32
  • 52

1 Answers1

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....

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • 1
    That's not what I meant. I've edited the question to make it more clear. "I'm trying to run this code only when "Default Web Site" doesn't exists." – galbarm Oct 18 '11 at 09:16