分享
 
 
 

用ADSI控制IIS创建网站, 虚拟目录…… (C#)

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

这是当年赶工的网站用到的东东, 结果快做完了警察叔叔来个网站要备案,朋友不干了,我也停工了,

翻出来共享了~

据说,控制IIS和别的MS的垃圾(MS的FTP, 用户管理
..)可以用ADSI和WMI(WIN2k3的才好),

参考了网友们的资料,有版权问题麻烦EMAIL一下

原来是按三层写的代码没有整理, 权当笔记,省点稿纸, 大家看个大概, 详细的MSDN都有!

三个文件:

/**/////////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

{

Private fields#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

Properties#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(id, 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 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

{

Private fields#region Private fields

private ModuleSettings settings;

private WebSiteInfo siteInfo;

#endregion

Properties#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 in 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);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有