Unable to Re-Host Website in IIS 6.0 in Windows Server 2003 server using C#
<blockquote>
  <p>Environment: Visual Studio 2008/SQL Server 2005</p>
  
  <p>Operating System: Windows Server 2003</p>
</blockquote>

<p>I am using below code to Create and Host Website in IIS.
In first time, Website Created and deleted successfully, While Recreating/Hosting Website in IIS 6.0 throws Error in below Line:</p>

<pre><code>object websiteId = (object)w3svc1.Invoke("CreateNewSite", newsite);
</code></pre>

<blockquote>
  <p>Error:</p>
  
  <p>Message = "Exception has been thrown by the target of an invocation."</p>
  
  <p>Inner Exception: InnerException = The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)</p>
  
  <p>[System.Reflection.TargetInvocationException] = {"Exception has been thrown by the target of an invocation."}</p>
</blockquote>

<pre><code>DirectoryEntry w3svc1 = new DirectoryEntry("IIS://localhost/W3SVC");

//Create a website object array
object[] newsite = new object[] {
    "TelnetProbeService",
    new object[] { "*:90:" },
    "c:\Test"
};

//invoke IIsWebService.CreateNewSite
object websiteId = (object)w3svc1.Invoke("CreateNewSite", newsite);

w3svc1.Properties["ServerAutoStart"][0] = true;
w3svc1.Properties["AuthNTLM"][0] = true;
w3svc1.Properties["AccessScript"][0] = true;
w3svc1.Properties["AccessFlags"][0] = 513;
w3svc1.Properties["AuthAnonymous"][0] = true;
w3svc1.Properties["AccessRead"][0] = true;
w3svc1.Properties["AccessWrite"][0] = false;
w3svc1.Properties["AccessScript"][0] = true;
w3svc1.Properties["AppPoolId"].Value = @"DefaultAppPool";
w3svc1.CommitChanges();
</code></pre>

<p>I am using below code to Delete Website in IIS.</p>

<pre><code>DirectoryEntry w3svc = new DirectoryEntry(
    "IIS://localhost/W3SVC",
    "SYNAPSE\rcatsadmin",
    "Global@123",
    AuthenticationTypes.Anonymous);

foreach (DirectoryEntry server in w3svc.Children)
{
  if (server.SchemaClassName == "IIsWebServer")
  {
    if (server.Properties["ServerComment"][0].ToString() == "TelnetProbeService")
    {
      server.DeleteTree();
      server.CommitChanges(); //THROWING ERROR
    }
  }
}
</code></pre>

<p>I am also having ADMINISTRATOR rights on machine.
I am running some msi in between of deleting and recreating/hosting website in IIS 6.0</p>
