【asp精品源码栏目提醒】:网学会员鉴于大家对asp精品源码十分关注,论文会员在此为大家搜集整理了“C#操作IIS7程序池新建站点站点配置 - 其它资料”一文,供大家参考学习
最近在做一个 WEB 程序的安装包;对一些操作 IIS 进行一个简单的总结;主要包括对 IIS 进行站点的新建以及新建站点的 NET 版本的选择,还有针对 IIS7 程序池的托管模式以及版本的操作;首先要对 Microsoft.Web.Administration 进行引用,它主要是用来操作 IIS7; using System.DirectoryServicesusing Microsoft.Web.Administration 1:首先是对本版 IIS 的版本进行配置:DirectoryEntry getEntity newDirectoryEntryIIS://localhost/W3SVC/INFO string Version getEntity.PropertiesMajorIISVersionNumber.Value.ToString MessageBox.ShowIIS 版本为: Version 2:是判断程序池是存在; /// /// 判断程序池是否存在 /// ///程序池名称 /// true 存在 false 不存在 private bool IsAppPoolNamestring AppPoolName bool result false DirectoryEntry appPools newDirectoryEntryIIS://localhost/W3SVC/AppPools foreach DirectoryEntry getdir in appPools.Children if getdir.Name.EqualsAppPoolName result true return result 3:删除应用程序池 /// /// 删除指定程序池 /// ///程序池名称 /// true 删除成功 false 删除失败 private bool DeleteAppPoolstring AppPoolName bool result false DirectoryEntry appPools newDirectoryEntryIIS://localhost/W3SVC/AppPools foreach DirectoryEntry getdir in appPools.Children if getdir.Name.EqualsAppPoolName try getdir.DeleteTree result true catch result false return result 4:创建应用程序池 (对程序池的设置主要是针对 IIS7;IIS7 应用程序池托管模式主要包括集成跟经典模式,并进行 NET 版本的设置) string AppPoolName LamAppPool if IsAppPoolNameAppPoolName DirectoryEntry newpool DirectoryEntry appPools newDirectoryEntryIIS://localhost/W3SVC/AppPools newpool appPools.Children.AddAppPoolNameIIsApplicationPool newpool.CommitChanges MessageBox.ShowAppPoolName 程序池增加成功 endregion region 修改应用程序的配置包含托管模式及其 NET 运行版本 ServerManager sm new ServerManager sm.ApplicationPoolsAppPoolName.ManagedRuntimeVersion v4.0 sm.ApplicationPoolsAppPoolName.ManagedPipelineMode ManagedPipelineMode.Classic //托管模式 Integrated 为集成 Classic 为经典 sm.CommitChanges MessageBox.ShowAppPoolName 程序池托管管道模式: sm.ApplicationPoolsAppPoolName.ManagedPipelineMode.ToString 运行的 NET 版本为: sm.ApplicationPoolsAppPoolName.ManagedRuntimeVersion 运用 C代码来对 IIS7 程序池托管管道模式及版本进行修改; 针 5: 对 IIS6 的 NET 版进行设置因为此处我是用到 NET4.0 所以 V4.0.30319 若是 NET2.0 则在这进行修改 v2.0.50727 //启动 aspnet_regiis.exe 程序 string fileName Environment.GetEnvironmentVariablewindir Microsoft.NETFrameworkv4.0.30319aspnet_regiis.exe ProcessStartInfo startInfo newProcessStartInfofileName //处理目录路径 string path vdEntry.Path.ToUpper int index path.IndexOfW3SVC path path.Remove0 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 类型进行增加;下面主要是我们用到两个类型分别是:xamlxap IISOle.MimeMapClass NewMime new IISOle.MimeMapClass NewMime.Extension .xaml NewMime.MimeType application/xamlxml IISOle.MimeMapClass TwoMime new IISOle.MimeMapClass TwoMime.Extension .xap TwoMime.MimeType application/x-silverlight-app rootEntry.PropertiesMimeMap.AddNewMime rootEntry.PropertiesMimeMap.AddTwoMime rootEntry.CommitChanges 7:下面是做安装时一段对 IIS 进行操作的代码;兼容 IIS6 及 IIS7;新建虚拟目录并对相应的属性进行设置;对 IIS7 还进行新建程序池的程序;并设置程序池的配置;/// /// 创建网站 /// /// public void CreateNewWebSiteNewWebSiteInfo siteInfo if EnsureNewSiteEnavaiblesiteInfo.BindString throw new Exception该网站已存在 Environment.NewLine siteInfo.BindString DirectoryEntry rootEntry GetDirectoryEntryentPath newSiteNum GetNewWebSiteID DirectoryEntry newSiteEntry rootEntry.Children.AddnewSiteNum IIsWebServer newSiteEntry.CommitChanges newSiteEntry.PropertiesServerBindings.Value siteInfo.BindString newSiteEntry.PropertiesServerComment.Value siteInfo.CommentOfWebSite newSiteEntry.CommitChanges DirectoryEntry vdEntry newSiteEntry.Children.AddrootIIsWebVirtualDir vdEntry.CommitChanges string ChangWebPath siteInfo.WebPath.Trim.RemovesiteInfo.WebPath.Trim.LastIndexOf1 vdEntry.PropertiesPath.Value ChangWebPath vdEntry.InvokeAppCreate true//创建应用程序 vdEntry.PropertiesAccessRead0 true //设置读取权限 vdEntry.PropertiesAccessWrite0 true vdEntry.PropertiesAccessScript0 true//执行权限 vdEntry.PropertiesAccessExecute0 false vdEntry.PropertiesDefaultDoc0 Login.aspx//设置默认文档 vdEntry.PropertiesAppFriendlyName0 LabManager//应用程序名称 vdEntry.PropertiesAuthFlags0 1//0 表示不允许匿名访问1 表示就可以 3 为基本身份验证,7 为 windows 继承身份验证 vdEntry.CommitChanges //操作增加 MIME //IISOle.MimeMapClass NewMime new IISOle.MimeMapClass //NewMime.Extension .xaml NewMime.MimeType application/xamlxml //IISOle.MimeMapClass TwoMime new IISOle.MimeMapClass //TwoMime.Extension .xap TwoMime.MimeType application/x-silverlight-app //rootEntry.PropertiesMimeMap.AddNewMime //rootEntry.PropertiesMimeMap.AddTwoMime //rootEntry.CommitChanges region 针对 IIS7 DirectoryEntry getEntity newDirectoryEntryIIS://localhost/W3SVC/INFO int Versionint.ParsegetEntity.PropertiesMajorIISVersionNumber.Value.ToString if Version 6 region 创建应用程序池 string AppPoolName LabManager if IsAppPoolNameAppPoolName DirectoryEntry newpool DirectoryEntry appPools newDirectoryEntryIIS://localhost/W3SVC/AppPools newpool appPools.Children.AddAppPoolNameIIsApplicationPool newpool.CommitChanges endregion region 修改应用程序的配置包含托管模式及其 NET 运行版本 ServerManager sm new Server上一篇:传智播客 net培训精品就业班招生简章+课程体系表