Iis
Using System;
Using System.DirectoryServices;
Using System.Collections;
Using System.Text.RegularExpressions;
Using System.Text;
/**
* @author Wu Haiyan
* @email wuhy80-usual@yahoo.com
* 2004-6-25 First Edition
*/
Namespace Wuhy.toolbox
{
<summary>
This class is a static class. Used to implement basic operations for managing IIS.
There are two ways to manage IIS, one is ADSI and the other is WMI. Because of system limitations, you have to choose to use ADSI to implement the functionality.
This is a pity. WMI can be used to manage systems only when IIS 6 is used
One problem, though, is that I now feel that such a method would be better done locally. It is best not to execute remotely.
Because that requires a considerable amount of bandwidth, even remotely, it is recommended to perform in the same network segment
</summary>
public class Iisadminlib
{
Definition of #region Username,password,hostname
public static string HostName
{
Get
{
return hostName;
}
Set
{
HostName = value;
}
}
public static string UserName
{
Get
{
return userName;
}
Set
{
UserName = value;
}
}
public static string Password
{
Get
{
return password;
}
Set
{
if (username.length <= 1)
{
throw new ArgumentException ("No good user name has been specified.") Please specify user name first ");
}
Password = value;
}
}
public static void Remoteconfig (String hostName, String userName, string password)
{
HostName = HostName;
UserName = UserName;
Password = Password;
}
private static string hostName = "localhost";
private static string UserName;
private static string password;
#endregion
The method of constructing entry based on the path of #region
<summary>
Determines whether a remote server is based on a user name.
And then construct a different DirectoryEntry.
</summary>
<param name= "Entpath" >directoryentry path </param>
<returns> returns a DirectoryEntry instance </returns>
public static DirectoryEntry Getdirectoryentry (string entpath)
{
DirectoryEntry Ent;
if (UserName = null)
{
ent = new DirectoryEntry (Entpath);
}
Else
{
ent = new DirectoryEntry (Entpath, hostname+ "\" +username, Password, authenticationtypes.secure);
ent = new DirectoryEntry (Entpath, UserName, Password, authenticationtypes.secure);
}
Return ENT;
}
#endregion
#region ways to add and remove Web sites
<summary>
Create a new Web site. Configure according to the information that is passed over
</summary>
<param name= "Siteinfo" > Stores information on the new site </param>
public static void Createnewwebsite (Newwebsiteinfo siteinfo)
{
if (! Ensurenewsiteenavaible (siteinfo.bindstring))
{
throw new Duplicatedwebsiteexception ("already has a website like this." "+ Environment.NewLine + siteinfo.bindstring);
}
String entpath = String.Format ("Iis://{0}/w3svc", HostName);
DirectoryEntry rootentry = Getdirectoryentry (Entpath);
String newsitenum = Getnewwebsiteid ();
DirectoryEntry newsiteentry = RootEntry.Children.Add (Newsitenum, "IIsWebServer");
Newsiteentry.commitchanges ();
newsiteentry.properties["ServerBindings"]. Value = siteinfo.bindstring;
newsiteentry.properties["ServerComment"]. Value = Siteinfo.commentofwebsite;
Newsiteentry.commitchanges ();
DirectoryEntry vdentry = NEWSITEENTRY.CHILDREN.ADD ("root", "IIsWebVirtualDir");
Vdentry.commitchanges ();
vdentry.properties["Path"]. Value = Siteinfo.webpath;
Vdentry.commitchanges ();
}
<summary>
Deletes a web site. Delete according to site name.
</summary>
<param name= "SiteName" > website name </param>
public static void Deletewebsitebyname (String siteName)
{
String sitenum = Getwebsitenum (siteName);
String siteentpath = String.Format ("Iis://{0}/w3svc/{1}", HostName, Sitenum);
DirectoryEntry siteentry = Getdirectoryentry (Siteentpath);
String rootpath = String.Format ("Iis://{0}/w3svc", HostName);
DirectoryEntry rootentry = Getdirectoryentry (RootPath);
RootEntry.Children.Remove (Siteentry);
Rootentry.commitchanges ();
}
#endregion
#region the start and stop Web methods
public static void Startwebsite (String siteName)
{
String sitenum = Getwebsitenum (siteName);
String siteentpath = String.Format ("Iis://{0}/w3svc/{1}", HostName, Sitenum);
DirectoryEntry siteentry = Getdirectoryentry (Siteentpath);
Siteentry.invoke ("Start", new object[] {});
}
public static void Stopwebsite (String siteName)
{
String sitenum = Getwebsitenum (siteName);
String siteentpath = String.Format ("Iis://{0}/w3svc/{1}", HostName, Sitenum);
DirectoryEntry siteentry = Getdirectoryentry (Siteentpath);
Siteentry.invoke ("Stop", new object[] {});
}
#endregion
#region confirm that the site is the same
<summary>
Make sure that a new site is not the same as the existing site.
This prevents the illegal data from being stored in IIS.
</summary>
<param name= "Bindstr" > website Bonding information </param>
<returns> true for can create, false for cannot create </returns>
public static bool Ensurenewsiteenavaible (string bindstr)
{
String entpath = String.Format ("Iis://{0}/w3svc", HostName);
DirectoryEntry ent = Getdirectoryentry (Entpath);
foreach (DirectoryEntry Child in Ent.) Children)
{
if (child. schemaClassName = = "IIsWebServer")
{
if (child. properties["ServerBindings"]. Value!= null)
{
if (child. properties["ServerBindings"]. value.tostring () = = Bindstr)
{
return false;
}
}
}
}
return true;
}
#endregion
How to #region get a site number
<summary>
Gets the number of a Web site. According to the site's serverbindings or servercomment to determine the site number
</summary>
<param name= "SiteName" ></param>
<returns> return the number of the site </returns>
<exception cref= "Notfoundwebsiteexception" > indicates that no site was found </exception>
public static string Getwebsitenum (String siteName)
{
Regex regex = new Regex (siteName);
String tmpstr;
String entpath = String.Format ("Iis://{0}/w3svc", HostName);
DirectoryEntry ent = Getdirectoryentry (Entpath);
foreach (DirectoryEntry Child in Ent.) Children)
{
if (child. schemaClassName = = "IIsWebServer")
{
if (child. properties["ServerBindings"]. Value!= null)
{
TMPSTR = child. properties["ServerBindings"]. Value.tostring ();
if (regex). Match (TMPSTR). Success)
{
Return to child. Name;
}
}
if (child. properties["ServerComment"]. Value!= null)
{
TMPSTR = child. properties["ServerComment"]. Value.tostring ();
if (regex). Match (TMPSTR). Success)
{
Return to child. Name;
}
}
}
}
throw new Notfoundwebsiteexception ("did not find the site we want" + siteName);
}
#endregion
#region ways to get new site IDs
<summary>
Gets the smallest ID that can be used within the Web site system.
This is because each site needs to have a unique number, and the smaller the number the better.
There is no problem with the algorithm being tested.
</summary>
<returns> the smallest id</returns>
public static string Getnewwebsiteid ()
{
ArrayList list = new ArrayList ();
String tmpstr;
String entpath = String.Format ("Iis://{0}/w3svc", HostName);
DirectoryEntry ent = Getdirectoryentry (Entpath);
foreach (DirectoryEntry Child in Ent.) Children)
{
if (child. schemaClassName = = "IIsWebServer")
{
TMPSTR = child. Name.tostring ();
List. ADD (Convert.ToInt32 (TMPSTR));
}
}
List. Sort ();
int i = 1;
foreach (int j in list)
{
if (i = = j)
{
i++;
}
}
return i.ToString ();
}
#endregion
}
#region New Website Information structure
public struct Newwebsiteinfo
{
private string HostIP; The Hosts IP address
private string Portnum; The New Web Sites port.generally is "80"
private string Descofwebsite; The website says. The site name for the Web site generally. such as "www.dns.com.cn"
private string commentofwebsite;//site comment. Typically also the site name for the site.
private string Webpath; The home directory of the Web site. such as "E:\tmp"
Public Newwebsiteinfo (String hostip, String portnum, String descofwebsite, String commentofwebsite, String webpath)
{
This.hostip = HostIP;
This.portnum = Portnum;
This.descofwebsite = Descofwebsite;
This.commentofwebsite = Commentofwebsite;
This.webpath = Webpath;
}
public string bindstring
{
Get
{
Return String.Format ("{0}:{1}:{2}", HostIP, Portnum, descofwebsite);
}
}
public string Commentofwebsite
{
Get
{
return commentofwebsite;
}
}
public string Webpath
{
Get
{
return webpath;
}
}
}
#endregion
}