Using System;
Using System. DirectoryServices;
Using System. Collections;
Using System. Text. RegularExpressions;
Using System. Text;
/**
* @ Author Wu Haiyan
* @ Email wuhy80-usual@yahoo.com
*-6-25 first version
*/
Namespace Wuhy. ToolBox
{
/// <Summary>
/// This class is a static class. This interface is used to manage basic IIS operations.
/// There are two ways to manage IIS: ADSI and WMI. Due to System Restrictions, you have to use ADSI to implement the function.
/// This is a pity. WMI can be used to manage the system only when IIS 6 is used.
/// However, I think it is better to execute such a method locally. It is recommended that you do not execute it remotely.
/// Because this requires a considerable amount of bandwidth, even if remote execution is required, it is recommended to execute in the same network segment.
/// </Summary>
Public class IISAdminLib
{
# Region UserName, Password, and 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 ("the user name has not been specified. Specify the 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
# Region method for constructing an Entry based on the path
/// <Summary>
/// Determine whether the server is a remote server based on the user name.
/// Then construct different directoryentries
/// </Summary>
/// <Param name = "entPath"> directory of DirectoryEntry </param>
/// <Returns> the returned value is the 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
# How to add and delete a website using region
/// <Summary>
/// Create a new website. Configure Based on the passed information
/// </Summary>
/// <Param name = "siteInfo"> stores information about the new website. </param>
Public static void CreateNewWebSite (NewWebSiteInfo siteInfo)
{
If (! EnsureNewSiteEnavaible (siteInfo. BindString ))
{
Throw new DuplicatedWebSiteException ("You already have such a website. "+ 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>
/// Delete a website. Delete a Website Based on its 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 Start and Stop website 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 check whether the website is the same
/// <Summary>
/// Confirm that a new website is not the same as an existing one.
/// This prevents illegal data from being stored in IIS
/// </Summary>
/// <Param name = "bindStr"> website binding information </param>
/// <Returns> it can be created, but cannot be created if it is false. </returns>
Public static bool ensurenewsiteenavaile (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
# Region How To Get A website number
/// <Summary>
/// Obtain the ID of a website. Determine the website number based on the ServerBindings or ServerComment of the website.
/// </Summary>
/// <Param name = "siteName"> </param>
/// <Returns> return website id </returns>
/// <Exception cref = "NotFoundWebSiteException"> indicates that no website is 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 child. Name;
}
}
If (child. Properties ["ServerComment"]. Value! = Null)
{
TmpStr = child. Properties ["ServerComment"]. Value. ToString ();
If (regex. Match (tmpStr). Success)
{
Return child. Name;
}
}
}
}
Throw new NotFoundWebSiteException ("the site we want is not found" + siteName );
}
# Endregion
# Region method for obtaining the new website id
/// <Summary>
/// Obtain the smallest ID that can be used in the website system.
/// This is because each website requires a unique ID, and the smaller the number, the better.
/// The above algorithms have been tested and there is no problem.
/// </Summary>
/// <Returns> Minimum 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; // indicates the website. Generally, it is the website name. For example, "www.dns.com.cn"
Private string commentOfWebSite; // website comment. It is also the website name of the website.
Private string webPath; // The Home Directory of the website. For example, "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
}