C # new site, delete site function code

Source: Internet
Author: User
Tags foreach iis tostring
Functions | site

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+ "create Complete");

}

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 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+ "delete completed");
}

Get the site identifier based on the site name
How to #region get a site number
public string Getwebsitenum (string siteName)

{

Regex 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 to child. Name;
}
}

if (child. properties["ServerComment"]. Value!= null)

{
TMPSTR = child. properties["ServerComment"]. Value.tostring ();
if (regex). Match (TMPSTR). Success)
{
Return to 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; The website says. The site name for the Web site generally. such as "www.dns.com.cn"

private string commentofwebsite;//site comment. Typically also the site name for the site.

private string Webpath; The home directory of the Web site. such as "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.