C#操作IIS问题--C#程序中如何为IIS新增一个站点

taito 2007-02-16 11:01:09
C#操作IIS问题--C#程序中如何为IIS新增一个站点

注意:

是新增一个站点,不是虚拟目录哟。以及怎么设置端口号与主机头?
...全文
288 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2007-02-19
  • 打赏
  • 举报
回复
其它的都是辅助功能,只要
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;
vDirEntry.CommitChanges();

即可

或者

参考
http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspx
taito 2007-02-19
  • 打赏
  • 举报
回复
网上我也找到过这段代码,不过

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

这些引用的源代码在哪儿?
wshuangminlg 2007-02-18
  • 打赏
  • 举报
回复
大年初一 凌晨帮顶
my_infinity 2007-02-18
  • 打赏
  • 举报
回复
哦~橘子好厉害哟,mark
cangwu_lee 2007-02-18
  • 打赏
  • 举报
回复

// 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
{
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;
vDirEntry.CommitChanges();
return true;
}
// Get and return a new website ID of specify host
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



cangwu_lee 2007-02-18
  • 打赏
  • 举报
回复
using System;
using System.Data;
using Wooyea.WebSite.Modules.Hosts.DataAccess;
using Wooyea.WebSite.Modules.Hosts.Configuration;
namespace Wooyea.WebSite.Modules.Hosts.Business
{
public class HostService
{
#region Private fields
private ModuleSettings settings;
private int id;
private string name;
private string description;
private decimal price;
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(id, name, description, price, rootPath, ip);
}
}
}
//end hostservice.cs



cangwu_lee 2007-02-18
  • 打赏
  • 举报
回复
using System;
using System.Data;
using Wooyea.WebSite.Modules.Hosts.DataAccess;
using Wooyea.WebSite.Modules.Hosts.Configuration;
namespace Wooyea.WebSite.Modules.Hosts.Business
{
public class HostService
{
#region Private fields
private ModuleSettings settings;
private int id;
private string name;
private string description;
private decimal price;
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(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
{
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;
vDirEntry.CommitChanges();
return true;
}
// Get and return a new website ID of specify host
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
{
public class WebSite
{
private ModuleSettings settings;
private WebSiteInfo siteInfo;

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;
}
// Create a new data record in website table and another one in the cross table
public int CreateSite(int userID)
{

IISManager iis = new IISManager();
iis.CreateSite(siteInfo);
DataAccess.WebSites newSite = new WebSites(settings.ConnectionString);

return newSite.Add(siteInfo, userID);
}
}
}


flyingfz 2007-02-18
  • 打赏
  • 举报
回复
代码好长~~~~以后再看
jxf654 2007-02-18
  • 打赏
  • 举报
回复
up
jxf654 2007-02-17
  • 打赏
  • 举报
回复
up

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧