using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.Collections;
using System.Text;
using System.Diagnostics;
namespace FileInfo
{
public class IIS
{
//从活动目录中获取IIS对象
static DirectoryEntry iisRoot = new DirectoryEntry("IIS://localhost/W3SVC");
/// <summary>
/// 得到网站的id
/// </summary>
/// <returns></returns>
private static int GetWebSiteID()
{
int result = 1, tempSite = 1;
//得到iis根目录下所有的对象
DirectoryEntries dirEntries = iisRoot.Children;
foreach(DirectoryEntry dir in dirEntries)
{
if(dir.SchemaClassName=="IIsWebServer")
{
tempSite = Convert.ToInt32(dir.Name);
if (tempSite >= result)
result = tempSite + 1;
}
}
return result;
}
/// <summary>
/// 创建网站
/// </summary>
/// <param name="description">网站描述</param>
/// <param name="ip">ip地址</param>
/// <param name="port">网站端口号</param>
/// <param name="domain">网站域名</param>
/// <param name="path">网站指向的物理路径</param>
/// <returns></returns>
public static string CreateWebSite(string description,string ip,int port,string domain,string path)
{
//得到网站的id
int siteID = GetWebSiteID();
//创建IISWebServer对象
using (DirectoryEntry webSite = (DirectoryEntry)iisRoot.Invoke("Create", new object[] { "IIsWebServer", siteID }))
{
webSite.Invoke("Put", "ServerComment", description);//网站描述
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
webSite.Invoke("Put", "ServerBindings", headerStr);//绑定域名
//创建一个虚拟目录
using (DirectoryEntry siteVDir = webSite.Children.Add("Root", "IISWebVirtualDir"))
{
siteVDir.Properties["Path"][0] = path;//网站指向的物理路径
siteVDir.Properties["AccessRead"][0] = true;//读取
siteVDir.Properties["AccessScript"][0] = true;//可执行脚本。执行权限下拉菜单中
string ScriptMaps = @".asp,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cer,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cdx,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.asa,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.idc,C:\WINDOWS\system32\inetsrv\httpodbc.dll,5,GET,POST
.shtm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.shtml,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.stm,C:\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST
.asax,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ascx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ashx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.asmx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.aspx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.axd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.vsdisco,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.rem,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.soap,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.config,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cs,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.csproj,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vb,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vbproj,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.webinfo,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.licx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resources,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.xoml,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.rules,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.master,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.skin,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.compiled,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.browser,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.mdb,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.jsl,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vjsproj,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sitemap,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.msgx,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.ad,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.dd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cd,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.adprototype,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.lddprototype,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sdm,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sdmDocument,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldb,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.svc,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.mdf,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldf,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.java,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.exclude,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.refresh,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.html,c:\\windows\\microsoft.net\\framework\\v2.0.50727\\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG";
String[] ScriptMapsList = System.Text.RegularExpressions.Regex.Split(ScriptMaps, "\r\n ");
siteVDir.Properties["ScriptMaps"].Value = ScriptMapsList;//主目录-配置-映射 并使网站是net2.0的网站
siteVDir.Properties["AspEnableParentPaths"][0] = true; //父路径启用
siteVDir.Properties["AspScriptErrorSentToBrowser"][0] = false;//Web 服务器是否将调试细节写到客户端浏览器
siteVDir.Properties["DefaultDoc"][0] = "Default.aspx";//设置主目录
siteVDir.Properties["AnonymousPasswordSync"][0] = true;//是否启用匿名访问
//siteVDir.Properties["AnonymousUserName"][0] = "";//匿名访问用户名
//siteVDir.Properties["AnonymousUserPass"][0] = "";//匿名访问密码
if (CreateAppPool(description))//创建应用程序池
{
siteVDir.Properties["AppPoolId"][0] = description;//应用程序池
}
siteVDir.Properties["AppIsolated"][0] = 2;//值 0 表示应用程序在进程内运行,值 1 表示进程外,值 2 表示进程池。
siteVDir.Properties["AccessFlags"][0] = 513;//包含有用于配置文件访问权限的标志
iisRoot.Invoke("EnableWebServiceExtension", "ASP.NET v2.0.50727");//启用ASP.NET v2.0.50727
siteVDir.Invoke("AppCreate", true);//创建应用程序站点
siteVDir.CommitChanges();
////启动aspnet_iis.exe程序 使网站是net2.0的网站
//string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe";
//ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
////处理目录路径
//string path2 = siteVDir.Path.ToUpper();
//int index = path2.IndexOf("W3SVC");
//path2 = path2.Remove(0, index);
////启动aspnet_iis.exe程序,刷新教本映射
//startInfo.Arguments = "-s " + path2;
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//startInfo.UseShellExecute = false;
//startInfo.CreateNoWindow = true;
//startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
//Process process = new Process();
//process.StartInfo = startInfo;
//process.Start();
//process.WaitForExit();
}
webSite.CommitChanges();
}
return "1";
}
/// <summary>
/// 创建应用程序池
/// </summary>
/// <param name="AppPoolId">应用程序池名称</param>
/// <returns>是否创建成功</returns>
public static bool CreateAppPool(string AppPoolId)
{
string strAppPoolName = AppPoolId;
System.DirectoryServices.DirectoryEntry appPoolRoot = new System.DirectoryServices.DirectoryEntry(@"IIS://localhost/W3SVC/AppPools");
System.DirectoryServices.DirectoryEntry newAppPool = appPoolRoot.Children.Add(strAppPoolName, "IIsApplicationPool");
//如果指定标识,可以使用下面的方法,否则,可以直接执行 CommitChanges 方法:
// 0 = Local System(本地系统)
// 1 = Local Service(本地服务)
// 2 = Network Service(网络服务)
// 3 = Custom Identity(自定义配置)需要设置 WAMUserName 和 WAMUserPass 属性
newAppPool.Properties["AppPoolIdentityType"][0] = 3;
newAppPool.Properties["WAMUserName"][0] = @"VISTA\1"; //domain\用户,注意:此用户必须在IIS_WPG组中
newAppPool.Properties["WAMUserPass"][0] = "1 ";
//其它属性类似,如设置Web园数目:
newAppPool.Properties["MaxProcesses"][0] = 5;
newAppPool.CommitChanges();
return true;
}
}
}