.NET Framework Class Library
SiteCollection..::.Remove Method

Removes the specified site from the site collection.

Namespace:  Microsoft.Web.Administration
Assembly:  Microsoft.Web.Administration (in Microsoft.Web.Administration.dll)
Syntax

Visual Basic (Declaration)
Public Sub Remove ( _
    element As Site _
)
Visual Basic (Usage)
Dim instance As SiteCollection
Dim element As Site

instance.Remove(element)
C#
public void Remove(
    Site element
)
Visual C++
public:
void Remove(
    Site^ element
)
JScript
public function Remove(
    element : Site
)

Parameters

element
Type: Microsoft.Web.Administration..::.Site
The Site object to remove from the SiteCollection object.
Exceptions

ExceptionCondition
ArgumentNullException

element is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

The Site object is removed from the collection in memory during this call. However, to commit the site configuration to the configuration system, you will need to use the ServerManager class to perform the update.

Examples

The following example creates a site, removes a site, and updates the configuration system.

Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Web.Administration
Imports Microsoft.Web.Management

Namespace AdministrationSnippets
    Friend Class MicrosoftWebAdministrationSite
        ' Methods


...


'' Creates an site named HRWeb
Public Sub CreateSite()
    Me.CreateSiteByName("HRWeb")
End Sub
'' Creates a new site with the specified name
Public Sub CreateSiteByName(ByVal name As String)
    Dim path As String = ("C:\inetpub\" & name & "site")
    Dim invalid As Char() = SiteCollection.InvalidSiteNameCharacters
    '' Validate the site name
    If (name.IndexOfAny(invalid) > -1) Then
        Console.WriteLine("Invalid site name: {0}", name)
    End If
    '' Create the content directory if it doesn't exist.
    If Not Directory.Exists(path) Then
        Directory.CreateDirectory(path)
    End If
    '' Create a new site using the new directory and update the config
    Dim manager As New ServerManager
    Try
        '' Add this site                
        '' The site will need to be started manually.
        manager.Sites.Add(name, "http", "*:9090:", path).ServerAutoStart = False
        manager.CommitChanges()
        Console.WriteLine(("Site " & name & " added to ApplicationHost.config file."))
    Catch obj1 As Object
        '' A site with this binding already exists. Do not attempt
        '' to add a duplicate site.
    End Try
End Sub


...


'' Creates a site, then deletes it 
Public Sub RemoveSite()
    '' Create a site to delete first
    Me.CreateSiteByName("HRWeb")
    '' Delete the site just created
    Me.RemoveSiteByName("HRWeb")
End Sub
'' Deletes a new site based on the name
Public Sub RemoveSiteByName(ByVal siteName As String)
    Dim manager As New ServerManager
    Dim siteToRemove As Site = manager.Sites.Item(siteName)
    manager.Sites.Remove(siteToRemove)
    manager.CommitChanges()
    Console.WriteLine(("Site " & siteName & " removed from ApplicationHost.config file."))
End Sub


...


    End Class
End Namespace
C#
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;
using Microsoft.Web.Management;

namespace AdministrationSnippets
{
    class MicrosoftWebAdministrationSite
    {


...


// Creates an site named HRWeb
public void CreateSite()
{
    CreateSiteByName("HRWeb");
}

// Creates a new site with the specified name
public void CreateSiteByName(string name)
{
    string path = @"C:\inetpub\" + name + "site";

    // Validate the site name
    char[] invalid = SiteCollection.InvalidSiteNameCharacters();
    if (name.IndexOfAny(invalid) > -1)
    {
        Console.WriteLine("Invalid site name: {0}", name);
    }

    // Create the content directory if it doesn't exist.
    if (!System.IO.Directory.Exists(path))
    {
        System.IO.Directory.CreateDirectory(path);
    }

    // Create a new site using the new directory and update the config
    ServerManager manager = new ServerManager();
    try
    {   // Add this site.
        Site hrSite = manager.Sites.Add(name, "http", "*:9090:", path);
        // The site will need to be started manually.
        hrSite.ServerAutoStart = false;
        manager.CommitChanges();
        Console.WriteLine("Site " + name + " added to ApplicationHost.config file.");
    }
    catch
    {
        // A site with this binding already exists. Do not attempt
        // to add a duplicate site.
    }
}


...


// Creates a site, then deletes it 
public void RemoveSite()
{
    // Create a site to delete first
    CreateSiteByName("HRWeb");

    // Delete the site just created
    RemoveSiteByName("HRWeb");
}
// Deletes a new site based on the name
public void RemoveSiteByName(string siteName)
{
    ServerManager manager = new ServerManager();
    Site siteToRemove = manager.Sites[siteName];
    manager.Sites.Remove(siteToRemove);
    manager.CommitChanges();
    Console.WriteLine("Site " + siteName + " removed from ApplicationHost.config file.");
}


...


    }
}
See Also

Reference

Tags :


Page view tracker