Modify the home directory of a website in IIS

About

Internet Information Services can be managed programmatically. It is possible to change everything that you can change using the IIS Manager, and also some other things that you cannot do with the IIS Manager. The IIS SDK (see link below) is all the information you need to be able to automate many of your daily tasks, and be more efficient in your job.

You can for example modify the home directory of a website in IIS programmatically.
This sample code shows you how to do this using C# and ADSI. It is tested on Windows XP Professional, but should work fine on Windows 2000 and Windows Server 2003 as well. It is only example code.

The code can be downloaded from the link below.

The code


     1: using System.DirectoryServices;
     2: using System;
     3: 
     4: public class IISAdmin
     5: {
     6:    /// <summary>
     7:    /// Changes the home directory of a website
     8:    /// </summary>
     9:    /// <param name="id">The id of the website. </param>
    10:    /// <param name="newroot">The path to the new folder. </param>
    11:    public static void ModifyHomeDirectory(string id, string newroot)
    12:    {
    13:       DirectoryEntry site = new DirectoryEntry("IIS://localhost/w3svc/" + id + "/ROOT");
    14: 
    15:       //Write out the old home folder
    16:       Console.WriteLine("Old root: " + site.Properties["Path"][0]);
    17: 
    18:       //change the home folder
    19:       site.Properties["Path"][0] = newroot;
    20:        
    21:       //commit the changes
    22:       site.CommitChanges();
    23:      
    24:       //write out the new root
    25:       Console.WriteLine("New root: " + site.Properties["Path"][0]);
    26:    }
    27: }
    28: 
    29: public class TestApp
    30: {
    31:    public static void Main(string[] args)
    32:    {
    33:       IISAdmin.ModifyHomeDirectory("1", @"D:\web\Customer1234");
    34:    }
    35: }
    36: 


Links

Download the code
IIS Programmatic Administration SDK
Script Repository: Internet Information Server 6.0

Home | Copyright © 2002 - 2005 Kristofer Gäfvert