c#操作IIS帮助类,创建网站,创建应用程序池,添加默认文档
- 在本机的IIS创建Web网站
- 删除网站包括应用程序池
- 删除应用程序池
- 添加默认文档
- 删除默认文档
源码如下
[C#] 纯文本查看 复制代码 using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AutoIIS.Helper
{
public class IISHelper
{
/// <summary>
/// 在本机的IIS创建Web网站
/// </summary>
/// <param name="siteName">网站名</param>
/// <param name="bindingInfo">host <example>"*:80:myhost.com"</example></param>
/// <param name="physicalPath">网站路径</param>
/// <returns>1成功,-1不成功</returns>
public static int CreateSite(string siteName, string bindingInfo, string physicalPath)
{
return createSite(siteName, "http", bindingInfo, physicalPath, true, siteName, ProcessModelIdentityType.ApplicationPoolIdentity, null, null, ManagedPipelineMode.Integrated, null);
}
/// <summary>
/// 在本机的IIS创建Web网站
/// </summary>
/// <param name="siteName">网站名</param>
/// <param name="protocol">http头</param>
/// <param name="bindingInformation">网站例如<example>"*:80:[url]www.sufeinet.com[/url]"</example></param>
/// <param name="physicalPath">网站的路径</param>
/// <param name="createAppPool">是否创建应用程序程序池</param>
/// <param name="appPoolName">应用程序名</param>
/// <param name="identityType">标识</param>
/// <param name="appPoolUserName">用户名没有时用户名为Null即可</param>
/// <param name="appPoolPassword">密码</param>
/// <param name="appPoolPipelineMode">模式,经典还是集成</param>
/// <param name="managedRuntimeVersion">.net版本</param>
/// <returns>1成功,-1不成功</returns>
private static int createSite(string siteName, string protocol, string bindingInformation, string physicalPath, bool createAppPool, string appPoolName,
ProcessModelIdentityType identityType, string appPoolUserName, string appPoolPassword, ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion)
{
using (ServerManager mgr = new ServerManager())
{
//删除网站和应用程序池
DeleteSite(siteName);
Site site = mgr.Sites.Add(siteName, protocol, bindingInformation, physicalPath);
// PROVISION APPPOOL IF NEEDED
if (createAppPool)
{
ApplicationPool pool = mgr.ApplicationPools.Add(appPoolName);
if (pool.ProcessModel.IdentityType != identityType)
{
pool.ProcessModel.IdentityType = identityType;
}
if (!String.IsNullOrEmpty(appPoolUserName))
{
pool.ProcessModel.UserName = appPoolUserName;
pool.ProcessModel.Password = appPoolPassword;
}
if (appPoolPipelineMode != pool.ManagedPipelineMode)
{
pool.ManagedPipelineMode = appPoolPipelineMode;
}
site.Applications["/"].ApplicationPoolName = pool.Name;
}
if (site != null)
{
mgr.CommitChanges();
return 1;
}
}
return -1;
}
/// <summary>
/// 删除网站包括应用程序池
/// </summary>
/// <param name="siteName">网站名</param>
/// <param name="isAppPool">是否删除应用程序池默认为删除</param>
/// <returns>1成功,-1不存在</returns>
public static int DeleteSite(string siteName, Boolean isAppPool = true)
{
using (ServerManager mgr = new ServerManager())
{
//判断web应用程序是否存在
if (mgr.Sites[siteName] != null)
{
if (isAppPool)
{
//判断应用程序池是否存在
if (mgr.ApplicationPools[siteName] != null)
{
mgr.ApplicationPools.Remove(mgr.ApplicationPools[siteName]);
}
}
mgr.Sites.Remove(mgr.Sites[siteName]);
mgr.CommitChanges();
return 1;
}
else
{
return -1;
}
}
}
/// <summary>
/// 删除应用程序池
/// </summary>
/// <param name="appPoolName">应用程序池名</param>
/// <returns>1成功,-1不存在</returns>
public static int DeletePool(string appPoolName)
{
using (ServerManager mgr = new ServerManager())
{
ApplicationPool pool = mgr.ApplicationPools[appPoolName];
if (pool != null)
{
mgr.ApplicationPools.Remove(pool);
mgr.CommitChanges();
return 1;
}
else
{
return -1;
}
}
}
/// <summary>
/// 添加默认文档
/// </summary>
/// <param name="siteName">网站</param>
/// <param name="defaultDocName">默认文档名</param>
/// <returns>-1已存在 -2添加时时报错 1是添加成功</returns>
public static int AddDefaultDocument(string siteName, string defaultDocName)
{
using (ServerManager mgr = new ServerManager())
{
Configuration cfg = mgr.GetWebConfiguration(siteName);
ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
ConfigurationElementCollection filesCollection = filesElement.GetCollection();
foreach (ConfigurationElement elt in filesCollection)
{
if (elt.Attributes["value"].Value.ToString() == defaultDocName)
{
return -1;//添加时存在
}
}
try
{
//创建一个新的默认页
ConfigurationElement docElement = filesCollection.CreateElement();
docElement.SetAttributeValue("value", defaultDocName);
filesCollection.Add(docElement);
}
catch (Exception)
{
return -2;//添加时发生错误
}
mgr.CommitChanges();
}
return 1;//添加成功
}
/// <summary>
/// 删除默认文档
/// </summary>
/// <param name="siteName">网站</param>
/// <param name="defaultDocName">默认文档名</param>
/// <returns>-1不存在 -2删除时报错 1是删除成功</returns>
public static int DeleteDefaultDocument(string siteName, string defaultDocName)
{
using (ServerManager mgr = new ServerManager())
{
Configuration cfg = mgr.GetWebConfiguration(siteName);
ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
ConfigurationElementCollection filesCollection = filesElement.GetCollection();
//创建一个新的默认页
ConfigurationElement docElement = filesCollection.CreateElement();
bool isdefault = false;
//不存在则返回
foreach (ConfigurationElement elt in filesCollection)
{
if (elt.Attributes["value"].Value.ToString() == defaultDocName)
{
docElement = elt;
isdefault = true;
}
}
if (!isdefault)
{
return -1;//不存在
}
try
{
filesCollection.Remove(docElement);
}
catch (Exception)
{
return -2;//删除时发生错误
}
mgr.CommitChanges();
}
return 1;//删除成功
}
}
}
使用方法
[C#] 纯文本查看 复制代码 //添加默认页
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text += IISHelper.AddDefaultDocument("苏飞论坛", txtdefault.Text.Trim());
}
//创建网站
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text += IISHelper.CreateSite(txtsite.Text.Trim(), txtdomain.Text.Trim(), txtfile.Text.Trim());
}
//删除默认文档
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Text += IISHelper.DeleteDefaultDocument("苏飞论坛", txtdefault.Text.Trim());
}
//删除网站
private void button4_Click(object sender, EventArgs e)
{
richTextBox1.Text += IISHelper.DeleteSite(txtsite.Text.Trim());
}
|