Use C # To manipulate IIS

Source: Internet
Author: User

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>


/

Related Article

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.