[Sample of Apr 8th] Set IIS Web Virtual Directory Properties - Microsoft All-In-One Code Framework - Site Home - MSDN Blogs
Microsoft All-In-One Code Framework - Developers' Pain Killer

[Sample of Apr 8th] Set IIS Web Virtual Directory Properties

Rate This
  • Comments 1

 

Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSSetIISWebVirtualDirProper-cbe2c622

Today’s code sample demonstrates how to set the Application name and Path on Virtual Directory tab (Properties) in IIS 6 programmatically. We are using here System.DirectoryServices namespace. This namespace is used to manage Directory Services using ADSI, Active Directory Services Interface.  This ADSI Interface has the interfaces for IIS metabase.

The sample is written by Microsoft Support Escalation Engineer Shaleen Thapa.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

This sample application demonstrates how to set the Application name and Path on Virtual Directory tab (Properties) in IIS 6 programmatically. We are using here System.DirectoryServices namespace. This namespace is used to manage Directory Services using ADSI, Active Directory Services Interface.  This ADSI Interface has the interfaces for IIS metabase.
 
However, you cannot perform the following tasks unless you are using Windows XP Professional with Service Pack 2 or Windows Server 2003 with Service Pack 1. Doing so results in errors like "The directory cannot report the number of properties":
 
Enumerating through a collection of properties
 
Setting binary properties
 
Adding entries to a property collection
 
Deleting entries from a property collection
 
Moving or copying metabase nodes

Configuring MIME types or IP security properties

Running the Sample

You can execute this sample by creating the exe via Visual Studio. Before calling this application, you must have IIS 6.0 installed on your machine. After you execute this program you should see that there is a Virtual directory created with the name you specified.
 
This sample concentrates on how to provide the Application Name however it is needed to set the Path name of the virtual directory under IIS.

Using the Code

You can use this project to set the Application name for a virtual directory in IIS. This application demonstrates how to use System.DirectoryServices to use ADSI interface for IIS. Once we bind to the root of the IIS metabase, we create the IIsWebVirtualDir schema object. Once we get this object we set the Path and the Application’s friendly name.

string siteName = "HelloWorldSite"; 
string physicalPath = @"C:\HelloWorldSite"; 
  
//Using DirectoryEntry bind to the Root of the IIS metabase. 
using (DirectoryEntry rootEntry = new DirectoryEntry("IIS://localhost/W3SVC/1/Root")) 
{ 
    //Create your Web Virtual Directory 
    using (DirectoryEntry testDirectoryEntry = rootEntry.Children.Add(siteName, "IIsWebVirtualDir")) 
    { 
        //Closing & Disposing DirectoryEntry object 
        rootEntry.Close(); 
  
        //Give the physical path for the Virtual Directory 
        testDirectoryEntry.Properties["Path"][0] = physicalPath; 
  
        //Save back the changes back to the IIS metabase 
        testDirectoryEntry.CommitChanges(); 
  
        //We are going to set the Name of the Virtual Directory 
        //By setting it's AppFriendlyName property 
        testDirectoryEntry.Properties["AppFriendlyName"][0] = siteName; 
  
        //Save the changes back to the IIS metabase 
        testDirectoryEntry.CommitChanges(); 
  
        //Do a close on the DirectoryEntry object 
        testDirectoryEntry.Close(); 
    } 
} 

More Information

For more information on System.DirectoryServices for IIS Administrator, visit http://msdn.microsoft.com/en-us/library/ms525791(VS.90).aspx

Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post
  • I have recently joined classes to learn .NET and I would go back and share this information.

Page 1 of 1 (1 items)