C # create a website and delete the website function code

Source: Internet
Author: User

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Windows. Forms. design;
Using system. directoryservices;
Using system. reflection;
Using system. Text. regularexpressions;

// Add site code

Private void button2_click (Object sender, system. eventargs E)
{


String newservercomment = textbox1.text;
String newserverip = textbox2.text;
String newserverport = textbox3.text;
String newserverpath = textbox4.text;
String newserverheader = textbox5.text;
// Newwebsiteinfo siteinfo = new newwebsiteinfo (hostip, portnum, descofwebsite, commentofwebsite, webpath );
Newwebsiteinfo siteinfo = new newwebsiteinfo (newserverip, newserverport, newserverheader, newservercomment, newserverpath );
String entpath = "IIS: // localhost/W3SVC ";
Directoryentry rootentry = new directoryentry (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 ();

MessageBox. Show ("Site" + siteinfo. commentofwebsite + "created ");

}

// IIS Site query code
//// <Summary>
/// Get and return a new website ID of specify host
/// </Summary>
/// <Returns> the smallest new website ID of the host </returns>
Public String getnewwebsiteid ()
{
Arraylist idlist = new arraylist ();
String tmpstr;

String entrypath = "IIS: // localhost/W3SVC ";
Directoryentry entry = getdirectoryentry (entrypath );

Foreach (directoryentry child in entry. Children)
{
If (child. schemaclassname = "iiswebserver ")
{
Tmpstr = Child. Name. tostring ();
Idlist. Add (convert. toint32 (tmpstr ));
}
}

Idlist. Sort ();

Int I = 1;
Foreach (int id in idlist)
{
If (I = ID)
{
I ++;
}
}

Return I. tostring ();
}

// Delete the site code

Private void button3_click (Object sender, system. eventargs E)
{
String newservercomment = textbox1.text;
String newserverip = textbox2.text;
String newserverport = textbox3.text;
String newserverpath = textbox4.text;
String newserverheader = textbox5.text;
String newserverhost = textbox6.text;

String sitenum = getwebsitenum (newservercomment );
String rootpath = "IIS: // localhost/W3SVC ";
String siteentpath = rootpath + "/" + sitenum;
Directoryentry rootentry = getdirectoryentry (rootpath );
Directoryentry siteentry = getdirectoryentry (siteentpath );
Rootentry. Children. Remove (siteentry );
Rootentry. commitchanges ();
MessageBox. Show ("Site" + newservercomment + "deleted ");
}

// Obtain the site identifier based on the site name
# Region How To Get A website number
Public String getwebsitenum (string sitename)

{

RegEx = new RegEx (sitename );
String tmpstr;
String entpath = "IIS: // localhost/W3SVC ";
Directoryentry ent = new directoryentry (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;
}
}
}
}
Return "";

}
# Endregion

 

# Region new IIS Site 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.