Operate the IIS application pool

Source: Internet
Author: User

Using system;
Using system. directoryservices;
Using system. reflection;

Namespace adsix
{
///
/// Small class containing methods to configure IIS.
///
Class configiis
{
///
/// The main entry point for the application.
///
[Stathread]
// Main program portal, which can be used? I wrote all the functions for convenience.
Static void main (string [] ARGs)
{
String apppoolname = "myapppool ";
String newvdir1 = "myvdir ";
Directoryentry newvdir = createvdir (newvdir1 );

Createapppool (apppoolname );
Assignapppool (newvdir, apppoolname );

Configapppool ("stop", apppoolname );
}

// Create a virtual directory
Static directoryentry createvdir (string vdirname)
{
Directoryentry newvdir;
Directoryentry root = new
Directoryentry ("IIS: // localhost/w3svc/1/root ");
Newvdir = root. Children. Add (vdirname, "iiswebvirtualdir ");
Newvdir. properties ["path"] [0] = "C: // inetpub // wwwroot ";
Newvdir. properties ["accessscript"] [0] = true;
Newvdir. commitchanges ();
Return newvdir;
}

// Create a new application pool.
Static void createapppool (string apppoolname)
{
Directoryentry newpool;
Directoryentry apppools = new
Directoryentry ("IIS: // localhost/w3svc/apppools ");
Newpool = apppools. Children. Add (apppoolname, "iisapplicationpool ");
Newpool. commitchanges ();
}

Static void assignapppool (directoryentry newvdir, string apppoolname)
{
Object [] Param = {0, apppoolname, true };
Newvdir. Invoke ("appcreate3", Param );
}

// Method is used to manage the application pool. There are three methods: start, stop, and recycle.
Apppoolname is the name of the application pool.
Static void configapppool (string method, string apppoolname)
{
Directoryentry apppool = new
Directoryentry ("IIS: // localhost/w3svc/apppools ");
Directoryentry findpool =
Apppool. Children. Find (apppoolname, iisapplicationpool ");
Findpool. Invoke (method, null );
Apppool. commitchanges ();
Apppool. Close ();
}

// List of application pools
Static void apppoollist ()
{
Directoryentry apppool = new
Directoryentry ("IIS: // localhost/w3svc/apppools ");
Foreach (directoryentry A in apppool. Children)
{
Console. writeline (A. Name );
}
}

Private void vdirtoapppool ()
{
Directroryentry Vd = new
Directoryentry ("IIS: // localhost/w3svc/1/root/CCC ");
Console. writeline (VD. properties ["apppoolid"]. value. tostring ());
}
}

}

// ================================= IIS6 operation example ==================== ================================
Using system;
Using system. directoryservices;
Using system. collections;
Using system. Text. regularexpressions;
Using system. text;

Namespace wuhy. toolbox
{
///

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

///
/// Determine whether the server is a remote server based on the user name.
/// Then construct different directoryentries
///
/// Directoryentry path
/// The returned result is a 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 );
}
Return ent;

}

# Endregion
# How to add and delete a website using region

///
/// Create a new website. Configure Based on the passed information
///
/// Store the information of the new website

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 ();

}

///
/// Delete a website. Delete a Website Based on its name.
///
/// Website name
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

///
/// Confirm that a new website is not the same as an existing one.
/// This prevents illegal data from being stored in IIS
///
/// Website binding information
/// It can be created. If it is false, it cannot be created.

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
///
/// Obtain the ID of a website. Determine the Website Based on the serverbindings or servercomment of the website.
No.
///
///
/// Return the website ID
/// The website is not found.

Public static string getwebsitenum (string sitename)
{
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
///
/// 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.
///
/// 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 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
}

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.