Use C # manipulate IIS (code)

zhaozj2021-02-16  52

Using system.directoryservices; using system.collections; using system.text.regularexpress; using system.text;

/ ** * @Author Wu Haiyan * @Email wuhy80-usual@yahoo.com * 2004-6-25 The first version * / 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 only IIS 6 is used, it is possible to use WMI to manage the system // / but there is a problem that I also think that such 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 perform remote execution, it is also recommended to execute /// public class iisadminlib {#Region Username, Password, Hostname, PUBLIC STATIC STRING HOSTNAME {Get {returno Hostname;} set {hostname = value;}}

Public static string username {get {return username;} set {username = value;}}

Public static string password;} set {ing (username.length <= 1) {throw new argumentException ("Yes not specified a good user name. Please specify username");

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 Depending on whether the user name is determined if the user name is determined if the user name is determined. /// then construct a different DirectoryEntry out /// /// DirectoryEntry path /// Returning DirectoryEntry instance 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;} #ENDREGON

#Region Add, delete the website method ///

/// creates a new website. Configuration according to the information passed /// /// Store information for new sites public static void CreateNewwebsite (NewWebsiteInfo SiteInfo) {if (! EnSureNewsiteENAVAIBLE) SiteInfo.Bindstring) {throw new duplicatedWebsiteException ("There is already such 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 (); newsiteentry.commitchange

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. /// /// Website name public static void deleteWebsitebyName (string siteenum = getWebsitenum (Sitename); string siteentpath = String.Format ("IIS : // {0} / w3svc / {1} ", Hostname, SiteNum); DirectoryEntry Sitentry = 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 Site Method PUBLIC Static Void StartWebsite (String Sitenum = GetWebsitenum (Sitename); string sInamentPath = 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 ( SitentPath;

SiteEntry.invoke ("stop", new object [] {});} #endregion

#Region confirms whether the website is the same ///

/// Determines a new website is not the same as the existing website. /// This prevents illegal data from being stored in the IIS /// /// Website Bond Information /// can be created , is not create false 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;} #ENDREGON

#Region Gets a website number ///

/// Gets the number of a website. Determine the website number /// /// /// Number> //// < Exception cref = "notfoundwebsiteexception"> Indicates that the website Public Static String getWebsitenum (STRING SITENAME) {regex regex = new regex (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]. Value! = Null) {tmpstr = child.properties ["serverbindings"]. Value.toString (); if (regex.match (tmpswstr) .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 ("Did not find the site" siteName);} #ENDREGION

#Region Gets a new website ID method ///

/// 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. /// /// Minimum ID 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 site 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 comment. 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.com

Public string webpath {get {return weight;}}} #ENDREGON}

转载请注明原文地址:https://www.9cbs.com/read-20032.html

New Post(0)