C#操作IIS程序池及站点的创建配置(转)
原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html
最近在做一个WEB程序的安装包;对一些操作IIS进行一个简单的总结;主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作;首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7;
using System.DirectoryServices; using Microsoft.Web.Administration;
1:首先是对本版IIS的版本进行配置:
DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");string Version = getEntity.Properties["MajorIISVersionNumber"].Value.ToString();MessageBox.Show("IIS版本为:" + Version);
2:是判断程序池是存在;
/// <summary>/// 判断程序池是否存在/// </summary>/// <param name="AppPoolName">程序池名称</param>/// <returns>true存在 false不存在</returns>private bool IsAppPoolName(string AppPoolName){bool result = false;DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");foreach (DirectoryEntry getdir in appPools.Children){if (getdir.Name.Equals(AppPoolName)){result = true;}}return result;}
3:删除应用程序池
/// <summary>/// 删除指定程序池/// </summary>/// <param name="AppPoolName">程序池名称</param>/// <returns>true删除成功 false删除失败</returns>private bool DeleteAppPool(string AppPoolName){bool result = false;DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");foreach (DirectoryEntry getdir in appPools.Children){if (getdir.Name.Equals(AppPoolName)){try{getdir.DeleteTree();result = true;}catch{result = false;}}}return result;}
4:创建应用程序池 (对程序池的设置主要是针对IIS7;IIS7应用程序池托管模式主要包括集成跟经典模式,并进行NET版本的设置)
string AppPoolName = "LamAppPool";if (!IsAppPoolName(AppPoolName)){DirectoryEntry newpool;DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");newpool.CommitChanges();MessageBox.Show(AppPoolName + "程序池增加成功");}#endregion#region 修改应用程序的配置(包含托管模式及其NET运行版本)ServerManager sm = new ServerManager();sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典sm.CommitChanges();MessageBox.Show(AppPoolName + "程序池托管管道模式:" + sm.ApplicationPools[AppPoolName].ManagedPipelineMode.ToString() + "运行的NET版本为:" + sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion);
运用C#代码来对IIS7程序池托管管道模式及版本进行修改;
5:针对IIS6的NET版进行设置;因为此处我是用到NET4.0所以V4.0.30319 若是NET2.0则在这进行修改 v2.0.50727
//启动aspnet_regiis.exe程序 string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe";ProcessStartInfo startInfo = new ProcessStartInfo(fileName);//处理目录路径 string path = vdEntry.Path.ToUpper();int index = path.IndexOf("W3SVC");path = path.Remove(0, index);//启动ASPnet_iis.exe程序,刷新脚本映射 startInfo.Arguments = "-s " + path;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();string errors = process.StandardError.ReadToEnd();
6:平常我们可能还得对IIS中的MIME类型进行增加;下面主要是我们用到两个类型分别是:xaml,xap
IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml";IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app";rootEntry.Properties["MimeMap"].Add(NewMime);rootEntry.Properties["MimeMap"].Add(TwoMime);rootEntry.CommitChanges();
7:下面是做安装时一段对IIS进行操作的代码;兼容IIS6及IIS7;新建虚拟目录并对相应的属性进行设置;对IIS7还进行新建程序池的程序;并设置程序池的配置;
/// <summary>/// 创建网站/// </summary>/// <param name="siteInfo"></param>public void CreateNewWebSite(NewWebSiteInfo siteInfo){if (!EnsureNewSiteEnavaible(siteInfo.BindString)){throw new Exception("该网站已存在" + Environment.NewLine + siteInfo.BindString);}DirectoryEntry rootEntry = GetDirectoryEntry(entPath);newSiteNum = GetNewWebSiteID();DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");newSiteEntry.CommitChanges();newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString;newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite;newSiteEntry.CommitChanges();DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");vdEntry.CommitChanges();string ChangWebPath = siteInfo.WebPath.Trim().Remove(siteInfo.WebPath.Trim().LastIndexOf('\\'),1);vdEntry.Properties["Path"].Value = ChangWebPath;vdEntry.Invoke("AppCreate", true);//创建应用程序vdEntry.Properties["AccessRead"][0] = true; //设置读取权限vdEntry.Properties["AccessWrite"][0] = true;vdEntry.Properties["AccessScript"][0] = true;//执行权限vdEntry.Properties["AccessExecute"][0] = false;vdEntry.Properties["DefaultDoc"][0] = "Login.aspx";//设置默认文档vdEntry.Properties["AppFriendlyName"][0] = "LabManager"; //应用程序名称 vdEntry.Properties["AuthFlags"][0] = 1;//0表示不允许匿名访问,1表示就可以3为基本身份验证,7为windows继承身份验证vdEntry.CommitChanges();//操作增加MIME//IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();//NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml";//IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();//TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app";//rootEntry.Properties["MimeMap"].Add(NewMime);//rootEntry.Properties["MimeMap"].Add(TwoMime);//rootEntry.CommitChanges();#region 针对IIS7DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");int Version =int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString());if (Version > 6){#region 创建应用程序池string AppPoolName = "LabManager";if (!IsAppPoolName(AppPoolName)){DirectoryEntry newpool;DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");newpool.CommitChanges();}#endregion#region 修改应用程序的配置(包含托管模式及其NET运行版本)ServerManager sm = new ServerManager();sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典sm.CommitChanges();#endregionvdEntry.Properties["AppPoolId"].Value = AppPoolName;vdEntry.CommitChanges();}#endregion//启动aspnet_regiis.exe程序 string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe";ProcessStartInfo startInfo = new ProcessStartInfo(fileName);//处理目录路径 string path = vdEntry.Path.ToUpper();int index = path.IndexOf("W3SVC");path = path.Remove(0, index);//启动ASPnet_iis.exe程序,刷新脚本映射 startInfo.Arguments = "-s " + path;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();string errors = process.StandardError.ReadToEnd();if (errors != string.Empty){throw new Exception(errors);}}
string entPath = String.Format("IIS://{0}/w3svc", "localhost");public DirectoryEntry GetDirectoryEntry(string entPath){DirectoryEntry ent = new DirectoryEntry(entPath);return ent;}public class NewWebSiteInfo{private string hostIP; // 主机IPprivate string portNum; // 网站端口号private string descOfWebSite; // 网站表示。一般为网站的网站名。例如"www.dns.com.cn"private string commentOfWebSite;// 网站注释。一般也为网站的网站名。private string webPath; // 网站的主目录。例如"e:\ mp"public NewWebSiteInfo(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath){this.hostIP = hostIP;this.portNum = portNum;this.descOfWebSite = descOfWebSite;this.commentOfWebSite = commentOfWebSite;this.webPath = webPath;}public string BindString{get{return String.Format("{0}:{1}:{2}", hostIP, portNum, descOfWebSite); //网站标识(IP,端口,主机头值)}}public string PortNum{get{return portNum;}}public string CommentOfWebSite{get{return commentOfWebSite;}}public string WebPath{get{return webPath;}}}
8:下面的代码是对文件夹权限进行设置,下面代码是创建Everyone 并给予全部权限
/// <summary>/// 设置文件夹权限 处理给EVERONE赋予所有权限/// </summary>/// <param name="FileAdd">文件夹路径</param>public void SetFileRole(){string FileAdd = this.Context.Parameters["installdir"].ToString();FileAdd = FileAdd.Remove(FileAdd.LastIndexOf('\\'), 1);DirectorySecurity fSec = new DirectorySecurity();fSec.AddAccessRule(new FileSystemAccessRule("Everyone",FileSystemRights.FullControl,InheritanceFlags.ContainerInherit|InheritanceFlags.ObjectInherit,PropagationFlags.None,AccessControlType.Allow));System.IO.Directory.SetAccessControl(FileAdd, fSec);}
转载于:https://www.cnblogs.com/Shadow3627/p/3216419.html
C#操作IIS程序池及站点的创建配置(转)相关推荐
- C#操作IIS创建应用程序池出现异常:无效索引(Exception from HRESULT:0x80070585)
在使用C#操作IIS创建应用程序池出现异常:无效索引(Exception from HRESULT:0x80070585) 相关代码: public static string CreateAppPo ...
- IIS中应用程序池和站点通过命令启停方法
最近在做灾备项目中,需要通过脚本方式对IIS服务中的应用程序池和站点,进行启动.停止操作,下面记录命令的使用方法 使用C:\Windows\System32\inetsrv\appcmd.exe 命令 ...
- C#中操作IIS 7.0
C#中操作IIS 7.0 作者:Jaxu 出处:博客园 2009-3-26 10:17:52 阅读 53 次 <script type="text/javascript" ...
- c# 操作服务器虚拟目录,C# 操作IIS服务器Demo
原标题:C# 操作IIS服务器Demo using System; using System.Collections; using System.Collections.Generic; using ...
- Windows:让Windows XP中的IIS支持多站点的工具
众所周知,在XP professional中使用IIS不能创建多个站点,虽然我们可以使用"虚拟目录"来调试程序,但有很多时候并不方便.今天无意中发现一个可以创建多站点的工具-IIS ...
- Net中如何操作IIS
Net中实际上已经为我们在这方面做得很好了.FCL中提供了不少的类来帮助我们完成这项工作,让我们的开发工作变非常简单和快乐.编程控制IIS实际上很简单,和ASP一样,.Net中需要使用ADSI来操作I ...
- asp.net操作IIS主机头的问题总结
今天发现一个怪怪的问题,在程序自动操作IIS主机头的问题上,在本地测试时没有任何问题,新增,删除主机头都正常.但传到服务器上时就有问题了,在新增主机头时没问题,删除主机头时就出现了这样的提示:拒绝访问 ...
- web服务器创建站点,Dreamweaver创建Web站点的六个步骤流程
对于一个SEO优化人员来说,虽然不要求对于网站建站有多么精通,但是还是必须要懂得一些使用建站工具的基本操作.在Dreamweaver中,Web站点可视为网站中所有文件的集合.我们可以在本地计算机上创建 ...
- python界面设置-PYTHON图形化操作界面的编程七__创建菜单
PYTHON图形化操作界面的编程七__创建菜单 十八.创建菜单 1.水平菜单的创建 创建菜单需要多条语句,所以这里通过实例来说明水平菜单的创建方法: 下面的语句可以在窗口中添加水平菜单,其中前四行语句 ...
最新文章
- poj2065 SETI
- (JAVA学习笔记) 关于稀疏数组
- bean的属性类型----ibatis类型-------oracle数据库类型
- RHEL6 LAMT TOCAT与APACHE整合
- git bash命令_更优雅地使用命令行
- Java 入门基础——面向对象的特征
- 安卓apk签名提取工具_Android测试工具入门介绍(二)
- MySQL CPU性能定位
- [ssh] remote host key has changed
- python测试rabbitmq简易实例
- 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用6
- 【渝粤教育】国家开放大学2018年秋季 0363-21T市场调查与预测 参考试题
- WiFi之WL工具命令
- 模块四:应急预案参考模板
- 文本表示与文本特征提取的区别
- postgresql下载linux版本
- Linux内核同步原语之信号量(Semaphore)
- 投之家与妙优车达成战略合作,加强推进优质资产端建设
- 标准化学校考场自动校时同步时钟系统
- Android.bp 语法和使用