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
{
///
/// This class is a static class. Used to implement the basic operation of managing IIS.
/// Manage IIS has two ways, one is ADSI, one is WMI. Due to system restrictions, you have to choose to use ADSI implementation.
/// This is a regret. Only when you only use IIS 6, it is possible to use WMI to manage the system.
// / But there is a question is that I also think that a method will be better in the local execution. It is best not to do it remotely.
/// Because it is necessary to occupy a considerable amount of bandwidth, even if you want to be executed remotely, it is also recommended in the same network segment.
/// summary>
Public Class IisadminLib
{
#REGON UserName, Password, Hostname definition
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
{
UserName.Length <= 1)
{
Throw new argumentException ("Yes not specified a good user name. Please specify the username first");
}
Password = Value;
}
}
Public Static Void RemoteConfig (String Hostname, String Password)
{
Hostname = hostname;
Username = username;
Password = password;
}
Private static string hostname = "localhost";
Private static string username;
Private static string password;
#ndregion
#Region method based on path constructing Entry
///
/ / According to whether or not the user name is to determine if it is a remote server.
/// then construct a different Directoryentry
/// summary>
/// DirectoryEntry path param>
///
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;
}
#ndregion
#Region Add, Remove Site Method
///
/// Create a new website. Configure according to the information passed
/// summary>
/// Store information for new sites param>
Public Static Void CreateNewwebsite (NewWebsiteInfo SiteInfo)
{
IF (! EnSureNewsiteenavaible (SiteInfo.Bindstring))
{
Throw new duplicatedWebsiteException ("There is already a website." Environment.newline SiteInfo.Bindstring;
}
String Entpath = String.Format ("IIS: // {0} / W3SVC", HostName);
DirectoryEntry Rootentry = getDirectoryEntry (ENTPATH);
String newsitenum = getNewwebsiteId ();
DirectoryEntry NewsItem = 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 ();
}
///
/// Delete a website. Delete according to the website name.
/// summary>
/// Website name param>
Public static void deleteWebsiteByName (String Sitename)
{
String Sitenum = getWebsitenum (Sitename);
String SitentPath = 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 ();
}
#ndregion
#REGION START and STOP website
Public Static Void StartWebsite (String Sitename)
{
String Sitenum = getWebsitenum (Sitename);
String SitentPath = 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 SitentPath = String.Format ("IIS: // {0} / w3svc / {1}", hostname, sitenum);
DirectoryEntry SiteEntry = getDirectoryEntry (SiteEntPath);
SiteEntry.Invoke ("stop", new object [] {});
}
#ndregion
#REGON to confirm that the website is the same
///
/// Determine a new website is not the same as the existing website.
/// This prevents illegal data from being stored in IIS
/// summary>
/// Website Bond Information param>
///
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;
}
#ndregion
#Region How to get a website number
///
/// Get the number of a website. Determine the website number based on the website serverbindings or servercomment
/// summary>
/// param>
///
///
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 ("No site we want" SiteName);
}
#ndregion
#Region How to get a new website ID
///
/// Get the smallest ID you can use in the website system.
// This is because each website needs to have a unique number, and the smaller this number, the better.
The algorithm in // This is not a problem.
/// summary>
///
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.toTOString ();
}
#ndregion
}
#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; // Website representation. Generally, the website name of the website. For example, "www.dns.com.cn"
Private string commentofwebsite; // website comments. Generally also also for the website of the website.
Private string WebPath; // The main 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;
}
}
}
#ndregion
}