using System; 
using Microsoft.Web.Administration; 
using System.IO; 
 
public class Program 
{ 
    private static string siteName = "MySite"; 
    private static string appName = "/App1"; 
    private static string vdName = "/VDir1"; 
    private static string appPoolName = "MyAppPool"; 
    private static string physicalAddress = "d:\\MyApp"; 
 
    static void Main(string[] args) 
    { 
        ///////////////////////////////////////////////////////////////////// 
        // Create a ServerManager instance: iisManager 
        // 
 
        ServerManager iisServerManager = new ServerManager(); 
 
 
        ///////////////////////////////////////////////////////////////////// 
        // Make sure the directory "d:\\MyApp" existed.  
        // 
 
        if (!Directory.Exists(physicalAddress)) 
            Directory.CreateDirectory(physicalAddress); 
 
 
        ///////////////////////////////////////////////////////////////////// 
        // Reset environment, remove first if exsit 
        // 
 
        // Delete web site 
        if (iisServerManager.Sites[siteName] != null) 
        { 
            iisServerManager.Sites.Remove(iisServerManager.Sites[siteName]); 
        } 
 
        // Delete application pool 
        if (iisServerManager.ApplicationPools[appPoolName] != null) 
        { 
            iisServerManager.ApplicationPools.Remove(iisServerManager. 
                ApplicationPools[appPoolName]); 
        } 

        iisServerManager.CommitChanges(); 
 
 
        ///////////////////////////////////////////////////////////////////// 
        // Create a IIS7 Site  
        // 
 
        Site site = iisServerManager.Sites.Add(siteName, physicalAddress, 8080); 
        iisServerManager.CommitChanges(); 
 
 
        ///////////////////////////////////////////////////////////////////// 
        // Create a IIS7 Appilication  
        // 
 
        iisServerManager.Sites[siteName].Applications.Add("/" + appName, physicalAddress); 
        iisServerManager.CommitChanges(); 

 
        ///////////////////////////////////////////////////////////////////// 
        // Remove a IIS7 Appilication  
        // 

        Site site = iisServerManager.Sites[siteName];
        Application application =  site.Applications["/RpoPrTracker"];
        site.Applications.Remove(application);
        mgr.CommitChanges();


        ///////////////////////////////////////////////////////////////////// 
        // Get Root Applicaition's Physical Path of a Site
        // 
        string rootAppPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
 
 
        ///////////////////////////////////////////////////////////////////// 
        // Create a IIS7 Virtual Directory  
        // 

        iisServerManager.Sites[siteName].Applications[appName]. 
            VirtualDirectories.Add("/" + vdName, physicalAddress); 
        iisServerManager.CommitChanges(); 
 
 
        ///////////////////////////////////////////////////////////////////// 
        // Create a app pool and set the new create site's app pool to this one.  
        // 
 
        iisServerManager.ApplicationPools.Add(appPoolName); 
        iisServerManager.Sites[siteName].Applications[appName]. 
            ApplicationPoolName = appPoolName; 
        ApplicationPool appPool = iisServerManager.ApplicationPools[appPoolName]; 
        appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated; 
        iisServerManager.CommitChanges(); 
 
 
        /////////////////////////////////////////////////////////////////////// 
        // Reset environment 
        // 
 
        // Delete web site 
        if (iisServerManager.Sites[siteName] != null) 
        { 
            iisServerManager.Sites.Remove(iisServerManager.Sites[siteName]); 
        } 
 
        // Delete application pool 
        if (iisServerManager.ApplicationPools[appPoolName] != null) 
        { 
            iisServerManager.ApplicationPools.Remove(iisServerManager. 
                ApplicationPools[appPoolName]); 
        } 
 
        iisServerManager.CommitChanges(); 
    } 
}