.net 对IIS的管理,如添加域名,绑定目录,绑定主机头
2012-05-15 17:22
351 查看
注意使用时要有服务器管理员权限 ,可在Web.config 添加
<system.web>
<identity impersonate="true" userName="服务器用户名" password="密码" />
</system.web>
using System;
using System.DirectoryServices;
using System.EnterpriseServices;
using System.Text.RegularExpressions;
namespace WebCloud.Builder
{
public class IIS
{
/// <summary>
/// 获取最小ID,越小越好
/// </summary>
/// <returns></returns>
public int SiteId()
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
// Find unused ID value for new web site
int siteID = 1;
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(e.Name);
if (ID >= siteID)
{
siteID = ID + 1;
}
}
}
return siteID;
}
#region 建IIS站点
/// <summary>
/// IIS站点
/// </summary>
/// <param name="port">站点端口</param>
/// <param name="siteName">站点ID</param>
/// <param name="siteExplain">域名</param>
/// <param name="defaultDoc">默认文档</param>
public int CreateSite(string port, string siteName, string siteExplain, string defaultDoc, string pathToRoot,string UserId)
{
int mark = 0;
try
{
// createAppPool(siteExplain);
DirectoryEntry de = new DirectoryEntry("IIS://localhost/" + "w3svc"); //从活动目录中获取IIS对象。
object[] prams = new object[2] { "IIsWebServer", Convert.ToInt32(siteName) };
DirectoryEntry site = (DirectoryEntry)de.Invoke("Create", prams); //创建IISWebServer对象。
site.Properties["KeyType"][0] = "IIsWebServer";
site.Properties["ServerComment"][0] = siteExplain; //站点说明
site.Properties["ServerState"][0] = 2; //站点初始状态,1.停止,2.启动,3
site.Properties["ServerSize"][0] = 1;
site.Properties["ServerBindings"].Add(":" + port + ":" + siteExplain); //站点端口
site.CommitChanges(); //保存改变
de.CommitChanges();
DirectoryEntry root = site.Children.Add("Root", "IIsWebVirtualDir"); //添加虚拟目录对象
root.Invoke("AppCreate", true); //创建IIS应用程序
root.Invoke("AppCreate3", new object[] { 2, UserId, true }); //创建应用程序池,并指定应用程序池为"HostPool","true"表示如果HostPool不存在,则自动创建
root.Properties["path"][0] = pathToRoot; //虚拟目录指向的物理目录
root.Properties["EnableDirBrowsing"][0] = true;//目录浏览
root.Properties["AuthAnonymous"][0] = true;
root.Properties["AccessExecute"][0] = true; //可执行权限
root.Properties["AccessRead"][0] = true;
root.Properties["AccessWrite"][0] = true;
root.Properties["AccessScript"][0] = true;//纯脚本
root.Properties["AccessSource"][0] = false;
root.Properties["FrontPageWeb"][0] = false;
root.Properties["KeyType"][0] = "IIsWebVirtualDir";
root.Properties["AppFriendlyName"][0] = siteExplain; //应用程序名
root.Properties["AppIsolated"][0] = 2;
root.Properties["DefaultDoc"][0] = defaultDoc; //默认文档
root.Properties["EnableDefaultDoc"][0] = true; //是否启用默认文档
root.CommitChanges();
site.CommitChanges();
root.Close();
site.Close();
de.CommitChanges(); //保存
site.Invoke("Start", null); //除了在创建过程中置初始状态外,也可在此调用方法改变状态
mark = 1;
}
catch
{
mark = 0;
}
return mark;
}
#endregion
#region 域名绑定方法
public int AddHostHeader(int siteid, string ip, int port, string domain)//增加主机头(站点编号.ip.端口.域名)
{
int mark = 0;
try
{
DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteid);
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
if (!serverBindings.Contains(headerStr))
{
serverBindings.Add(headerStr);
}
site.CommitChanges();
mark = 1;
}
catch
{
mark = 0;
}
return mark;
}
#endregion
#region 删除主机头
public void DeleteHostHeader(int siteid, string ip, int port, string domain)//删除主机头(站点编号.ip.端口.域名)
{
DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteid);
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
if (serverBindings.Contains(headerStr))
{
serverBindings.Remove(headerStr);
}
site.CommitChanges();
}
#endregion
#region 删除站点
public void DelSite(string siteName)
{
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", "localhost", siteNum);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
string rootPath = String.Format("IIS://{0}/w3svc", "localhost");
DirectoryEntry rootEntry = new DirectoryEntry(rootPath);
rootEntry.Children.Remove(siteEntry);
rootEntry.CommitChanges();
}
public string GetWebSiteNum(string siteName)
{
Regex regex = new Regex(siteName);
string tmpStr;
string entPath = String.Format("IIS://{0}/w3svc", "localhost");
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 创建应用程序池
static void createAppPool(string AppPoolName)
{
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.CommitChanges();
}
#endregion
#region 删除应用程序池
public void deleteAppPool(string AppPoolName)
{
bool ExistAppPoolFlag=false;
try
{
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach (DirectoryEntry a in apppools.Children)
{
if (a.Name == AppPoolName)
{
ExistAppPoolFlag = true;
a.DeleteTree();
// MessageBox.Show("应用程序池名称删除成功", "删除成功");
}
}
if (ExistAppPoolFlag == false)
{
// MessageBox.Show("应用程序池未找到", "删除失败");
}
}
catch
{
//MessageBox.Show(ex.Message, "错误");
}
}
#endregion
}
}
<system.web>
<identity impersonate="true" userName="服务器用户名" password="密码" />
</system.web>
using System;
using System.DirectoryServices;
using System.EnterpriseServices;
using System.Text.RegularExpressions;
namespace WebCloud.Builder
{
public class IIS
{
/// <summary>
/// 获取最小ID,越小越好
/// </summary>
/// <returns></returns>
public int SiteId()
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
// Find unused ID value for new web site
int siteID = 1;
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(e.Name);
if (ID >= siteID)
{
siteID = ID + 1;
}
}
}
return siteID;
}
#region 建IIS站点
/// <summary>
/// IIS站点
/// </summary>
/// <param name="port">站点端口</param>
/// <param name="siteName">站点ID</param>
/// <param name="siteExplain">域名</param>
/// <param name="defaultDoc">默认文档</param>
public int CreateSite(string port, string siteName, string siteExplain, string defaultDoc, string pathToRoot,string UserId)
{
int mark = 0;
try
{
// createAppPool(siteExplain);
DirectoryEntry de = new DirectoryEntry("IIS://localhost/" + "w3svc"); //从活动目录中获取IIS对象。
object[] prams = new object[2] { "IIsWebServer", Convert.ToInt32(siteName) };
DirectoryEntry site = (DirectoryEntry)de.Invoke("Create", prams); //创建IISWebServer对象。
site.Properties["KeyType"][0] = "IIsWebServer";
site.Properties["ServerComment"][0] = siteExplain; //站点说明
site.Properties["ServerState"][0] = 2; //站点初始状态,1.停止,2.启动,3
site.Properties["ServerSize"][0] = 1;
site.Properties["ServerBindings"].Add(":" + port + ":" + siteExplain); //站点端口
site.CommitChanges(); //保存改变
de.CommitChanges();
DirectoryEntry root = site.Children.Add("Root", "IIsWebVirtualDir"); //添加虚拟目录对象
root.Invoke("AppCreate", true); //创建IIS应用程序
root.Invoke("AppCreate3", new object[] { 2, UserId, true }); //创建应用程序池,并指定应用程序池为"HostPool","true"表示如果HostPool不存在,则自动创建
root.Properties["path"][0] = pathToRoot; //虚拟目录指向的物理目录
root.Properties["EnableDirBrowsing"][0] = true;//目录浏览
root.Properties["AuthAnonymous"][0] = true;
root.Properties["AccessExecute"][0] = true; //可执行权限
root.Properties["AccessRead"][0] = true;
root.Properties["AccessWrite"][0] = true;
root.Properties["AccessScript"][0] = true;//纯脚本
root.Properties["AccessSource"][0] = false;
root.Properties["FrontPageWeb"][0] = false;
root.Properties["KeyType"][0] = "IIsWebVirtualDir";
root.Properties["AppFriendlyName"][0] = siteExplain; //应用程序名
root.Properties["AppIsolated"][0] = 2;
root.Properties["DefaultDoc"][0] = defaultDoc; //默认文档
root.Properties["EnableDefaultDoc"][0] = true; //是否启用默认文档
root.CommitChanges();
site.CommitChanges();
root.Close();
site.Close();
de.CommitChanges(); //保存
site.Invoke("Start", null); //除了在创建过程中置初始状态外,也可在此调用方法改变状态
mark = 1;
}
catch
{
mark = 0;
}
return mark;
}
#endregion
#region 域名绑定方法
public int AddHostHeader(int siteid, string ip, int port, string domain)//增加主机头(站点编号.ip.端口.域名)
{
int mark = 0;
try
{
DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteid);
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
if (!serverBindings.Contains(headerStr))
{
serverBindings.Add(headerStr);
}
site.CommitChanges();
mark = 1;
}
catch
{
mark = 0;
}
return mark;
}
#endregion
#region 删除主机头
public void DeleteHostHeader(int siteid, string ip, int port, string domain)//删除主机头(站点编号.ip.端口.域名)
{
DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteid);
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
if (serverBindings.Contains(headerStr))
{
serverBindings.Remove(headerStr);
}
site.CommitChanges();
}
#endregion
#region 删除站点
public void DelSite(string siteName)
{
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", "localhost", siteNum);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
string rootPath = String.Format("IIS://{0}/w3svc", "localhost");
DirectoryEntry rootEntry = new DirectoryEntry(rootPath);
rootEntry.Children.Remove(siteEntry);
rootEntry.CommitChanges();
}
public string GetWebSiteNum(string siteName)
{
Regex regex = new Regex(siteName);
string tmpStr;
string entPath = String.Format("IIS://{0}/w3svc", "localhost");
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 创建应用程序池
static void createAppPool(string AppPoolName)
{
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.CommitChanges();
}
#endregion
#region 删除应用程序池
public void deleteAppPool(string AppPoolName)
{
bool ExistAppPoolFlag=false;
try
{
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach (DirectoryEntry a in apppools.Children)
{
if (a.Name == AppPoolName)
{
ExistAppPoolFlag = true;
a.DeleteTree();
// MessageBox.Show("应用程序池名称删除成功", "删除成功");
}
}
if (ExistAppPoolFlag == false)
{
// MessageBox.Show("应用程序池未找到", "删除失败");
}
}
catch
{
//MessageBox.Show(ex.Message, "错误");
}
}
#endregion
}
}
相关文章推荐
- .net C# 程序控制IIS 添加站点域名绑定
- tomcat配置多域名访问非webapps下面多工程目录(类似IIS主机头)
- IIS7多域名绑定同一物理目录,设置不同默认文档的解决方案
- IIS环境下如何批量添加、修改、删除绑定的域名
- wampserver2.5绑定域名添加虚拟主机以及伪静态设置
- 虚拟主机不支持域名绑定二级子目录(完美解决方法)
- CP How-To:如何使用cPanel添加Addon Domains(绑定域名到目录)
- 同一主机,不同域名绑定不同网站(IIS主机头实现方法)
- IIS7多域名绑定同一物理目录,设置不同默认文档的解决方案
- 阿里云云服务器Windows 2008下IIS添加网站绑定域名图文教程
- 同一主机,不同域名绑定不同网站(IIS主机头实现方法)
- yii2.0域名目录绑定(二级域名)以及url美化 url伪静态 Apache ,Ngnix和 IIS
- IIS7多域名绑定同一物理目录,设置不同默认文档的最佳解决方案
- IIS 之 IIS 7及以上多域名或端口绑定同一物理目录并设置不同默认文档
- Tomcat 配置 项目 到tomcat目录外面 和 域名绑定访问(api接口、前端网站、后台管理网站)
- IIS7多域名绑定同一物理目录,设置不同默认文档的解决方案
- 虚拟主机下实现多域名绑定不同的子目录的方法
- IIS 之 添加绑定域名 或 设置输入IP直接访问网站
- 阿里云ECS主机绑定域名
- Tomcat发布网站(修改端口、修改默认发布目录、多域名绑定)