DCSIMG
How to create an application in IIS6 using C# - David Birin's blog

How to create an application in IIS6 using C#

Before the IIS7 era, we had no API for directly manipulating IIS, but we still needed to perform some actions like creating a virtual directory or an application (mostly used in installations).

Creating an application using the IIS GUI is done in the following way:

IIS-CreateApp

 

Creating it in code is a more complex task which is dived into 3 parts:

1. Finding the website in the IIS which we want to create the application underneath:

/// <summary>
/// This function looks for the path of a web site in IIS
/// </summary>
/// <param name="siteName">The name of the website we are looking for</param>
/// <returns>The requested path or String.Empty if not found</returns>
public string GetAdsiPathForSite(string siteName)
{
    try
    {
        DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/W3SVC");
        // Go over all the nodes in the iis
        foreach (DirectoryEntry site in entry.Children)
        {
            //Check if the node is a website (it can be ftp and more...)
            if (site.SchemaClassName == "IIsWebServer")
            {
                // site.Properties["ServerComment"].Value stores the website name
                object tempObj = site.Properties["ServerComment"].Value;
                string serverComments = "";
                if (tempObj != null)
                {
                    serverComments = tempObj.ToString();
                }
                //Compare the name of the website to the name we got as parameters
                if (serverComments == siteName)
                {
                  
                    return site.Path;
                }
 
            }
        }
    }
    catch (Exception ex)
    {
       //Do some exception handling
    }
 
    return string.Empty;
}

2. Creating a virtual directory (if not exists), you can’t create an application on a folder which is not virtual directory:

/// <summary>
/// This function creates an virtual directory on an IIS site
/// </summary>
/// <param name="parentPath">The path to the directory (without the directory name)</param>
/// <param name="name">The name of the directory</param>
public void CreateWebDirectory(string parentPath, string name)
{
    try
    {
        if (EntryExists(parentPath + "/" + name))
        {
            //Virtual dirctory already exists on IIS, there is no need to create one
            return;
        }
        using (DirectoryEntry entry = new DirectoryEntry(parentPath))
        {
            //Invoking the "Create" method which creates the virtual directory
            DirectoryEntry newEntry = (DirectoryEntry)entry.Invoke("Create", new
            object[2] { "IIsWebDirectory", name });
            newEntry.CommitChanges();
        }
      
    }
    catch (Exception ex)
    {
        //Do some exception handling
    }
}
 
/// <summary>
/// This fuction check if a virtual direcory exists in IIS
/// </summary>
/// <param name="path">The path to the virtual directory</param>
/// <returns>True if exists False otherwise</returns>
public bool EntryExists(string path)
{
    try
    {
        using (DirectoryEntry entry = new DirectoryEntry(path))
        {
            return entry.Guid != Guid.Empty;
        }
    }
    catch
    {
        return false;
    }
}

3. Create the application:

/// <summary>
/// This function creates an application from virtual directory
/// </summary>
/// <param name="path">The path to the virtual directory</param>
/// <param name="name">The name of the new application</param>
public void CreateApplication(string path, string name)
{
    try
    {
        using (DirectoryEntry entry = new DirectoryEntry(path))
        {
            //Invoking the "AppCreate2" method which creates the application
            entry.Invoke("AppCreate2", new object[1] { 3 });
            entry.Properties["AppFriendlyName"].Value = name;
            entry.CommitChanges();
        }   
    }
    catch (Exception ex)
    {
        //Do some exception handling;
    }
}

It took some time to resolve the above because it’s undocumented, I hope it will help you.

David Birin

Published 17 December 2008 12:39 PM by DavidBi
תגים:, ,

Comments

# Arnon said on 17 December, 2008 03:49 PM

Thanks dave, that was very useful !!

# Avi Pinto said on 18 December, 2008 08:07 AM

תודה

# Owen said on 15 May, 2009 02:46 AM

Thanks heaps! Really helpful. I've spent ages trying to figure out how to do what you did in step 3.

# kumar said on 29 May, 2009 02:01 PM

i am using iis 5.1, this code is working fine with 5.1 when i deploy this in iis 7, it was giving error. please help me,

# DavidBi said on 30 May, 2009 10:58 AM

To kumar:

IIS 7 has a whole new API, you can read more about it at learn.iis.net/.../how-to-use-microsoftwebadministration

# Chiranjeev said on 20 October, 2009 12:03 PM

I am bit amateur,

can somebody please explain or paste the code to how to create the virtual directory. I mean to say complete code in order.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: