C # List all IIS sites and related site properties

Source: Internet
Author: User
iis| site

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Windows.Forms.Design;
Using System.DirectoryServices;
Using System.Reflection;
Using System.Text.RegularExpressions;

int totalservercount=0;

DirectoryEntry RootFolder = new DirectoryEntry ("Iis://localhost/w3svc");
Totalservercount=rootfolder. Children.SchemaFilter.Count;

foreach (DirectoryEntry Child in RootFolder.) Children)
{
if (child. schemaClassName = = "IIsWebServer")
{
Totalservercount+=1;
}
}
Loop to get all site detail properties to write to the array
string [] Arrayserverid = new string[totalservercount];//site identifier
string [] Arrayserverip = new string[totalservercount];//Site Host Header
string [] Arrayserverport = new string[totalservercount];//Site Host Header
string [] Arrayserverheader = new string[totalservercount];//Site Host Header
string [] Arrayserverpath = new string[totalservercount];//Site Host Header
string [] arrayservercomment = new string[totalservercount];//Site Host Header

string [] arrayserverbinds = new string[totalservercount];//Site Host Header


String currentserverbindings;//binding Host header IP Port string
Char[] a= ":". ToCharArray ();
string [] currentbingdings =new string[2];

int i=0;
foreach (DirectoryEntry Child in RootFolder.) Children)
{
if (child. schemaClassName = = "IIsWebServer")
{
Arrayserverid.setvalue (child. Name.tostring (), i);
Arrayservercomment.setvalue (child. properties["ServerComment"]. Value.tostring (), i);

Currentserverbindings=child. properties["ServerBindings"]. Value.tostring ();

Currentbingdings=currentserverbindings.split (a);

Arrayserverip.setvalue (Currentbingdings[0],i);
Arrayserverport.setvalue (Currentbingdings[1],i);
Arrayserverheader.setvalue (Currentbingdings[2],i);


foreach (DirectoryEntry rootchild in child. Children)
{
if ((Rootchild.schemaclassname = = "IIsWebVirtualDir") && (rootChild.Name.ToString () = = "Root")
{
if (rootchild.properties["Path"). Value==null)
{
Arrayserverpath.setvalue ("", I);
}
Else
{
Arrayserverpath.setvalue (rootchild.properties["Path"). Value.tostring (), i);
}
}
}

I+=1;
}
}


Write to the DataGrid.
Loop reads data from an array
for (i=0;i<totalservercount;i++)
{

LISTVIEW1.ITEMS.ADD ((i+1). ToString ());
Listview1.items[i]. SubItems.Add (Arrayserverid.getvalue (i). ToString ());
Listview1.items[i]. SubItems.Add (Arrayservercomment.getvalue (i). ToString ());
Listview1.items[i]. SubItems.Add (Arrayserverip.getvalue (i). ToString ());
Listview1.items[i]. SubItems.Add (Arrayserverport.getvalue (i). ToString ());
Listview1.items[i]. SubItems.Add (Arrayserverheader.getvalue (i). ToString ());
Listview1.items[i]. SubItems.Add (Arrayserverpath.getvalue (i). ToString ());


}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.