Test your technical skills online, appear in Virtual Interview and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Make friends on DotNetFunda.com and expand your network!
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Articles Author
Sun, 20-Apr-2014 Authors
All Time Authors
Sourav.Kayal
39750
SheoNarayan
34800
Niladri.biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads

Modification of IIS Metabase in C#

Suhailnabi
Posted by under ASP.NET category on for Intermediate level | Views : 7505 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
1 vote(s)


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.


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

You can call this function like

    CreateNewVirtualDirectory(1, "MyNewVirtualDirectory", @"C:\Inetpub\wwwroot\MyDir", true)


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.
Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:




About the Author

Suhailnabi

Full Name: suhail banday
Member Level: Starter
Member Status: Member
Member Since: 2/28/2008 3:21:51 AM
Country:

i am workin as a Dotnet Professional email id :- suhailnabi@gmail.com
Login to vote for this post.

Comments or Responses


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Comment using Facebook