C#配置IIS搭建网站的工具类
public class IISWorker
{
public static string HostName = "localhost"; /// <summary>
/// 获取本地IIS版本
/// </summary>
/// <returns></returns>
public static string GetIIsVersion()
{
try
{
DirectoryEntry entry = new DirectoryEntry("IIS://" + HostName + "/W3SVC/INFO");
string version = entry.Properties["MajorIISVersionNumber"].Value.ToString();
return version;
}
catch (Exception se)
{
//说明一点:IIS5.0中没有(int)entry.Properties["MajorIISVersionNumber"].Value;属性,将抛出异常 证明版本为 5.0
return string.Empty;
}
} /// <summary>
/// 创建虚拟目录网站
/// </summary>
/// <param name="webSiteName">网站名称</param>
/// <param name="physicalPath">物理路径</param>
/// <param name="domainPort">站点+端口,如192.168.1.23:90</param>
/// <param name="isCreateAppPool">是否创建新的应用程序池</param>
/// <returns></returns>
public static int CreateWebSite(string webSiteName, string physicalPath, string domainPort, bool isCreateAppPool)
{
DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");
// 为新WEB站点查找一个未使用的ID
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; }
}
}
// 创建WEB站点
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
site.Invoke("Put", "ServerComment", webSiteName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", domainPort);
//site.Invoke("Put", "ServerBindings", "127.0.0.1:80:"+"poolcutwcf");
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", "Default.html");
site.Invoke("Put", "SecureBindings", "127.0.0.1:443:abs-dit.goldenstand.cn");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
// 创建应用程序虚拟目录 DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
siteVDir.Properties["AppIsolated"][0] = 2;
siteVDir.Properties["Path"][0] = physicalPath;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["FrontPageWeb"][0] = 1;
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";
siteVDir.Properties["AppFriendlyName"][0] = "Root"; // 创建应用程序TaskProcessEngine
DirectoryEntry de = siteVDir.Children.Add("TaskProcessEngine", siteVDir.SchemaClassName);
de.CommitChanges();
de.Properties["Path"].Value = physicalPath + "/TaskProcessEngine";
de.Properties["AccessRead"][0] = true; // 勾选读取
de.Invoke("AppCreate", true);
de.Properties["EnableDefaultDoc"][0] = true;
de.Properties["AccessScript"][0] = true; // 脚本资源访问
de.Properties["DontLog"][0] = true; // 勾选记录访问
de.Properties["ContentIndexed"][0] = true; // 勾选索引资源
de.Properties["AppFriendlyName"][0] = "TaskProcessEngine"; //应用程序名
de.Properties["AuthFlags"][0] = 5; if (isCreateAppPool)
{
DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools"); //DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");
DirectoryEntry newpool = apppools.Children.Add("TSSAppPool", "IIsApplicationPool");
//newpool.Properties["AppPoolIdentityType"][0] = "4"; //4
newpool.Properties["AppPoolIdentityType"][0] = "1"; //4
newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式
newpool.CommitChanges();
//siteVDir.Properties["AppPoolId"][0] = webSiteName;
siteVDir.Properties["AppPoolId"][0] = "TSSAppPool";
} siteVDir.CommitChanges();
site.CommitChanges();
return siteID;
} /// <summary>
/// 得到网站的物理路径
/// </summary>
/// <param name="rootEntry">网站节点</param>
/// <returns></returns>
public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)
{
string physicalPath = "";
foreach (DirectoryEntry childEntry in rootEntry.Children)
{
if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))
{
if (childEntry.Properties["Path"].Value != null)
{
physicalPath = childEntry.Properties["Path"].Value.ToString();
}
else
{
physicalPath = "";
}
}
}
return physicalPath;
} /// <summary>
/// 获取站点名
/// </summary>
public static List<IISInfo> GetServerBindings()
{
List<IISInfo> iisList = new List<IISInfo>();
string entPath = String.Format("IIS://{0}/w3svc", HostName);
DirectoryEntry ent = new DirectoryEntry(entPath);
foreach (DirectoryEntry child in ent.Children)
{
if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
{
if (child.Properties["ServerBindings"].Value != null)
{
object objectArr = child.Properties["ServerBindings"].Value;
string serverBindingStr = string.Empty;
if (IsArray(objectArr))//如果有多个绑定站点时
{
object[] objectToArr = (object[])objectArr;
serverBindingStr = objectToArr[0].ToString();
}
else//只有一个绑定站点
{
serverBindingStr = child.Properties["ServerBindings"].Value.ToString();
}
IISInfo iisInfo = new IISInfo();
iisInfo.DomainPort = serverBindingStr;
iisInfo.AppPool = child.Properties["AppPoolId"].Value.ToString();//应用程序池
iisList.Add(iisInfo);
}
}
}
return iisList;
} public static bool CreateAppPool(string appPoolName, string Username, string Password)
{
bool issucess = false;
try
{
//创建一个新程序池
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");
newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool"); //设置属性 访问用户名和密码 一般采取默认方式
newpool.Properties["WAMUserName"][0] = Username;
newpool.Properties["WAMUserPass"][0] = Password;
newpool.Properties["AppPoolIdentityType"][0] = "3";
newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式
newpool.CommitChanges();
issucess = true;
return issucess;
}
catch // (Exception ex)
{
return false;
}
} /// <summary>
/// 建立程序池后关联相应应用程序及虚拟目录
/// </summary>
public static void SetAppToPool(string appname, string poolName)
{
//获取目录
DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry getentity in getdir.Children)
{
if (getentity.SchemaClassName.Equals("IIsWebServer"))
{
//设置应用程序程序池 先获得应用程序 在设定应用程序程序池
//第一次测试根目录
foreach (DirectoryEntry getchild in getentity.Children)
{
if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
{
//找到指定的虚拟目录.
foreach (DirectoryEntry getsite in getchild.Children)
{
if (getsite.Name.Equals(appname))
{
//【测试成功通过】
getsite.Properties["AppPoolId"].Value = poolName;
getsite.CommitChanges();
}
}
}
}
}
}
} /// <summary>
/// 判断object对象是否为数组
/// </summary>
public static bool IsArray(object o)
{
return o is Array;
}
} public class IISInfo
{
public string DomainPort;
public string AppPool;
}
将上面两个类复制到代码中,引入相关的包,然后调用类中的方法即可。
转载自http://www.cnblogs.com/Gyoung/p/3574611.html
C#配置IIS搭建网站的工具类的更多相关文章
- windows server2008 IIS搭建网站简易教程(阿里云)
最近搞了个阿里云服务器 想着需要用这个服务器学点东西,故而想着把自己之前写的网站能部署上去,虽然自己是做前端移动开发,但是对这个服务器的东西也很感兴趣 第一步 配置阿里云入口规则 如图开放81 80 ...
- Rhino+envjs-1.2.js 在java运行网站js 工具类
java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...
- 关于如何在本地IIS搭建网站
步骤一: 首先安装与配置IIS服务,可借用百度经验:http://jingyan.baidu.com/article/d5a880eb75f74713f047cc57.html 步骤二: 修改web. ...
- 002-基本业务搭建【日志,工具类dbutils,dbcp等使用】
一.需求分析 1.1.概述 1.用户进入“客户管理”,通过列表方式查看用户: 2.客户名称,模糊查询用户列表 3.客户名称,可查看客户详细信息 4.新增.编辑.删除功能等 二.系统设计 需要对原始需求 ...
- 分享自己配置的HttpURLConnection请求数据工具类
>>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...
- window8配置IIS,搭建应用程序网站。
这个里面的一定要勾选,不然会有莫名其妙的错误. 在应用程序池中,如果你是64位电脑,需勾选启用32位应用程序为 true
- 如何在阿里云服务器里配置iis 搭建web服务
IIS,互联网信息服务,一种Web服务组件,利用它,我们可以打开asp.php这些搭建网页所用的文件. 工具/原料 域名 服务器 方法/步骤 登录服务器. 点击开始—>服务器 ...
- IIS搭建网站(二)
win+IIS+ASP+ACCESS第二种搭建方式 安装 控制面板”,依次选“添加/删除程序”, 添加/删除Windows组件 在应用程序服务器前打钩.点击详细信息 将“Internet信息服务(II ...
- 配置IIS让网站可以播放mp4文件
最近遇到这么一个问题,网站当中的mp4不能播放了--每次点击播放的时候都会产生404的错误(如下图).这个问题来得有些蹊跷,因为在这台服务器上其他的文件都能正常执行,比如xml.jpg.aspx等文件 ...
随机推荐
- 16位模式/32位模式下PUSH指令探究——《x86汇编语言:从实模式到保护模式》读书笔记16
一.Intel 32 位处理器的工作模式 如上图所示,Intel 32 位处理器有3种工作模式. (1)实模式:工作方式相当于一个8086 (2)保护模式:提供支持多任务环境的工作方式,建立保护机制 ...
- js 屏蔽浏览器事件汇总
//js 屏蔽 window.document.oncontextmenu = function () { event.returnValue = false; }//屏蔽鼠标右键 window.do ...
- JVM(四) G1 收集器工作原理介绍
此篇文章半原创是对参考资料中的知识点进行总结,欢迎评论指点,谢谢! 部分知识点总结来自R大的帖子,下文有参考资料的链接 概述 G1 收集是相比于其他收集器(可见 上一篇文章),可以独立运 ...
- C#基础 (一)
值类型和引用类型 堆和栈 栈存放的数据: (1)某些类型变量的值(2)程序当前的执行环境(3)传递给方法的参数 堆是存放对象的地方 对象类型有两种: 值类型和引用类型,他们的存储方式不同值类型: 只需 ...
- 三年从前端小工到架构-知乎 Live 学习整理
最近在知乎上学习了vczero (王利华,簋谣)的知乎Live「三年从前端小工到架构」,感觉受益匪浅,现将本次Live学习笔记记录如下. 本次 Live 主要包括以下内容 • 0-3 年的前端工程 ...
- iframe适应屏幕大小,隐藏滚动条,移动端
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- ES6,先知道这些必会的才行
变量声明 const 和 let 不要用 var,而是用 const 和 let,分别表示常量和变量.不同于 var 的函数作用域,const 和 let 都是块级作用域. const DELAY = ...
- Echarts按需引入后没有显示图例问题
因为Echarts官网的例子都是引入整个Echarts.js.如果使用按需引入对应模块就要记得引入legend模块,才能显示出图例. 例如这样: require("echarts/lib/c ...
- linux下nginx的安装及配置
一.安装nginx前,我们首先要确保系统安装了g++.gcc.openssl-devel.pcre-devel和zlib-devel软件,可通过如图所示命令进行检测,如果以安装我们可以通过图二所示卸载 ...
- USG防火墙基础
http://support.huawei.com/huaweiconnect/enterprise/thread-331003.html 华为防火墙产品线 安全区域 1. 默认防火墙区域 T ...