IIS Metabase and programmatic administration in C# by ibrahimuludag

Introduction

System.DirectoryServices namespace can be used to access Internet Information Service(IIS). You can create, delete and change the properties of web servers, virtual directories, directories and files. Also you can invoke IIS methods.
This article will tell basic concepts about IIS Metabase and some programmatic administration of IIS. The programmatic administration will include a tip about managing IIsWebDirectory and IIsWebFile.

IIS Metabase

IIS Metabase is a structure where IIS configuration settings are stored. The metabase configuration and schema for IIS 4.0 and IIS 5.0 were stored in a binary file, but from IIS6.0 the configuration and setting is stored in single binary file (MetaBase.bin), with plain text, Extensible Markup Language (XML)-formatted files named MetaBase.xml and MBSchema.xml.  You can navigate through the IIS Metabase using MetaEdit or Metabase Explorer.

The Metabase is based on a hierarchical design with inheritance. Each object in the metabase has a KeyType. The KeyType property specifies the type of metabase key.

Each property of an object has a set data type and can be configured at specific locations. For example, XoverTableFile can only have string value. And this property can be configured only at IIsNntpServer. You can fing the full list from MSDN.

Programmatic Administration

Let’s create a function to create a new virtual directory and set some properties of the new virtual directory.

public void CreateNewVirtualDirectory(int ServerId, string VirtualDirName, string Path, bool AccessScript){
	DirectoryEntry Parent = new DirectoryEntry(@"IIS://localhost/W3SVC/" + ServerId.ToString() + "/Root");
	DirectoryEntry NewVirtualDir;
	NewVirtualDir = Parent.Children.Add(VirtualDirName, "IIsWebVirtualDir");
	NewVirtualDir.Properties["Path"][0] = Path;
	NewVirtualDir.Properties["AccessScript"][0] = AccessScript;
	NewVirtualDir.CommitChanges();
}

You can call this function like

At the line 2, we are accessing the parent object that we will create the new virtual directory under. Every web server has a unique ServerId. At the line 4, we are adding the new virtual directory. At the line 5 and line 6, we are setting properties of the new virtual directory. Path property specifies the physical path associated with a virtual directory. AccessScript specifies if dymanic contect will be executed. (e.g. asp, aspx, cgi). At last, we must commit changes. Otherwise the canges will not be written to the metabase.

Now, let’s write a function to delete a virtual directory. We will use the Delete method of the IIS. The parameters of this method are KeyType and Object Name.

public void DeleteVirtualDirectory(int ServerId, string VirtualDirName)
{
	DirectoryEntry Parent = new DirectoryEntry(@"IIS://localhost/W3SVC/" + ServerId.ToString() + "/Root");
	Object[] Parameters = {"IIsWebVirtualDir",VirtualDirName};
	Parent.Invoke("Delete",Parameters );
}

Directories and Files

When you create a new directory or a file under a virtual directory, the directories or files are not created under the IIS Metabase. If you want to change the metabase properties of them, you will get an error like “The system cannot find the path specified”.

If you want to change the metabase properties of this directory or file, first you must create the directory or file under the IIS Metabase.

Directory Strategy

The path of the file or directory might be like /Dir1/Dir2/Dir3. So you must create each directory under IIS Metabase. You can use the function below to create IIsWebDirectory recursively.

You can call the function from your program, and change the properties.

System.DirectoryServices.DirectoryEntry De = new DirectoryEntry();
De.Path = @"IIS://localhost/W3SVC/1/ROOT/Dir1/Dir2/Dir3";
RecursiveSetIIsWebDirectory(De);
De.Properties["AccessFlags"][0] = 513;
De.CommitChanges();

File Strategy

For the file, the first object will be IIsWebFile, and all the parents will be IIsWebDirectory. For this, I have added depth parameter to the function. The function must be called with depth parameter equal to 0.

You can call the function from your program, and change the properties.

System.DirectoryServices.DirectoryEntry De = new DirectoryEntry();
De.Path = @"IIS://localhost/W3SVC/1/ROOT/Dir1/Dir2/Dir3/file.txt";
RecursiveSetIIsWebFile(De, 0);
De.Properties["AccessFlags"][0] = 513;
De.CommitChanges();

The RecursiveSetIIsWebDirectory and RecursiveSetIIsWebFile functions are attached in the sample attachments with explanation.

Attachments:

Project Files: iis_metabase_project.zip

Source Files: iis_metabase_src.zip