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等文件 ...
随机推荐
- Mybatis基本介绍
Mybatis介绍 1.Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了goog ...
- C++公有继承、保护继承和私有继承
C++中的继承方式有: public.private.protected三种(它们直接影响到派生类的成员.及其对象对基类成员访问的规则). (1)public(公有继承):继承时保持基类中各成员属性不 ...
- 巧用hover改变css样式和背景
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python-爬虫之requests模块介绍(登陆github)
介绍 使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) 注意 requests库发送请求将网页内容下载下来以后 ...
- 进入与退出anconda虚拟环境
# 进入虚拟环境,使用 # # $ conda activate spider-venv # # 退出虚拟环境,使用 # # $ conda deactivate
- AtCoder Grand Contest 006 F - Blackout
Description 在 \(n*n\) 的棋盘上给出 \(m\) 个黑点,若 \((x,y)\),\((y,z)\) 都是黑点,那么 \((z,x)\) 也会变成黑点,求最后黑点的数量 题面 So ...
- [转]Supporting OData Query Options in ASP.NET Web API 2
本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/suppor ...
- MYSQL连接字符串参数解析(解释)
被迫转到MySQL数据库,发现读取数据库时,tinyint类型的值都被转化为boolean了,这样大于1的值都丢失,变成true了.查阅资料MySQL中无Boolean类型,都是存储为tinyint了 ...
- 一:Shiro知识整理
一:springboot快速入门: 1.建立Maven项目,导入springboot父工程 <!-- 继承springboot默认父工程 --> <parent> <gr ...
- 2013 Warm up 3 -- Skill --- dp
题意:求n位数字,满足非递减的个数. dp[ i ] [ j ] = sum( dp[i -1] [ k ] ); k =>( j , 9); #include<iostream> ...