Use ADSI to control IIS to create Web sites, virtual directories ... (C #)

Source: Internet
Author: User
Tags foreach bool config iis tostring
iis| Create | control | Virtual Directory This is the site used to rush the stuff, the result is almost finished the police uncle to a website to record, friends do not do, I also shut down,

Turn it out and share it ~

It is said that control of IIS and other MS garbage (MS FTP, user management ...) You can use ADSI and WMI (Win2k3 is good),

Refer to the information of netizens, have copyright problem trouble email

The original is written according to the three-layer code is not collated, the right to take notes, save a little paper, we look at a general, detailed MSDN have!

Three files:

FileName:hostservice.cs

//////////////////////////////////////////////////////////////////////////////////////////////////

Using System;
Using System.Data;

Using Wooyea.WebSite.Modules.Hosts.DataAccess;
Using Wooyea.WebSite.Modules.Hosts.Configuration;

Namespace Wooyea.WebSite.Modules.Hosts.Business
{
<summary>
Summary description for Hostservice.
</summary>
public class Hostservice
{
#region Private Fields

private Modulesettings settings;

private int id;
private string name;
private string description;
private decimal price; The field in SQL server is the type of smallmoney
private string IP;
private int port;
private string RootPath;
private int maxBandwidth;
private int maxconnections;
private int cpulimit;
Private byte ServerSize;
Private byte appisolated;
#endregion

#region Properties

public int ID
{
get {return ID;}
set {id = value;}
}

public string Name
{
get {return name;}
set {name = value;}
}

public string Description
{
get {return description;}
Set {description = value;}
}

Public decimal Price
{
get {return price;}
set {price = value;}
}

public string IP
{
get {return IP;}
set {IP = value;}
}

public int Port
{
get {return port;}
set {port = value;}
}

public string RootPath
{
get {return rootpath;}
set {RootPath = value;}
}

public int MaxBandwidth
{
get {return maxBandwidth;}
set {maxBandwidth = value;}
}

public int CPULimit
{
get {return cpulimit;}
set {CPULimit = value;}
}

Public byte serversize
{
get {return serversize;}
set {serversize = value;}
}

#endregion

Public Hostservice ()
{
Configuration.moduleconfig config = new moduleconfig ();
Settings = config. GetSettings ();
}

Public Hostservice (string name, string description, decimal price, string IP, String rootpath): this ()
{
THIS.name = name;
this.description = description;
This.price = Price;
This.ip = IP;
This.rootpath = RootPath;

}

public hostservice (int id): this ()
{
This.id = ID;
Getdetails ();
}

public void Getdetails ()
{
Dataaccess.hostservices datahostservices = new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices (settings. ConnectionString);

DataRow Temprow = datahostservices.getdetails (ID);

THIS.name = (string) temprow["Hostservicename"];
This.price = Convert.todecimal (temprow["Hostserviceprice"));
This.ip = (string) temprow["Hostserviceip"];
This.port = (int) temprow["Hostserviceport"];
This.rootpath = (string) temprow["Hostservicerootpath"];

}

Public DataSet gethostservices ()
{
Dataaccess.hostservices datahostservices= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices (settings. ConnectionString);

return datahostservices.gethostservices ();
}

public int Create ()
{
Dataaccess.hostservices datahostservices= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices (settings. ConnectionString);

Return Datahostservices.insert (name, description, Price, RootPath, IP);
}

public bool Update ()
{
Dataaccess.hostservices datahostservices= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices (settings. ConnectionString);

Return Datahostservices.update (IDs, name, description, Price, RootPath, IP);
}

}
}
End hostservice.cs/////////////////////////////////////////////////////////////////////

FileName:IISManager.cs

Using System;
Using System.Collections;
Using System.Text.RegularExpressions;
Using System.Text;
Using System.DirectoryServices;

Using Wooyea.WebSite.Modules.Hosts;

Namespace Wooyea.WebSite.Modules.Hosts.Business
{
<summary>
Summary description for Class1.
</summary>
Class Iismanager
{

Public Iismanager ()
{}

Public DirectoryEntry Getdirectoryentry (string entrypath)
{
Creater Direntry instance depend on a local or remote
DirectoryEntry direntry = new DirectoryEntry (Entrypath);

return direntry;
}

public bool CreateSite (Websiteinfo newsiteinfo)
{


String Entpath = "Iis://localhost/w3svc";
DirectoryEntry rootentry = Getdirectoryentry (Entpath);

String Newsiteid = Getnewsiteid ();

DirectoryEntry newsiteentry = RootEntry.Children.Add (Newsiteid, "IIsWebServer");
Newsiteentry.commitchanges ();

newsiteentry.properties["ServerBindings"]. Value = newsiteinfo.serverbindings;
newsiteentry.properties["ServerComment"]. Value = newsiteinfo.comment;
Newsiteentry.commitchanges ();

DirectoryEntry vdirentry = NEWSITEENTRY.CHILDREN.ADD ("root", "IIsWebVirtualDir");
Vdirentry.commitchanges ();

vdirentry.properties["Path"].        Value = Newsiteinfo.path; ph PATTH in Disk
Vdirentry.commitchanges ();

return true;

}

<summary>
Get and return a new website ID of specify host
</summary>
<returns>the smallest new website ID of the host</returns>
public string Getnewsiteid ()
{
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 ();
}


}
}

End iismanager/////////////////////////////

File Name:WebSite.cs

Using System;
Using System.DirectoryServices;

Using Wooyea.WebSite.Modules.Hosts.Configuration;

Using Wooyea.WebSite.Modules.Hosts.Business;
Using Wooyea.WebSite.Modules.Hosts.DataAccess;

Namespace Wooyea.WebSite.Modules.Hosts.Business
{
<summary>
Summary description for WebSite.
</summary>
public class WebSite
{
#region Private Fields

private Modulesettings settings;

Private Websiteinfo Siteinfo;

#endregion

#region Properties

#endregion

WebSite ()
{
Configuration.moduleconfig config = new moduleconfig ();
Settings = config. GetSettings ();
}

Public WebSite (String newheader, String newcomment, string newip, int newPort, string newpath): this ()
{
This.siteInfo.Header = Newheader;
This.siteInfo.IP = NEWIP;
This.siteInfo.Port = NewPort;
This.siteInfo.Comment = NewComment;
This.siteInfo.Path = NewPath;
}
<summary>
Create a new data record with website table and another one in the cross table
</summary>
<param name= "UserID" >userid of current priciple</param>
<returns></returns>
public int createsite (int userID)
{


Iismanager IIS = new Iismanager ();
Iis. CreateSite (Siteinfo);

Dataaccess.websites newsite = new WebSites (settings. ConnectionString);

Return Newsite.add (Siteinfo, UserID);

}

}
}




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.