C#操作IIS方法集合
更新时间:2020年6月25日 11:26 点击:1905
C# 操作IIS方法集合
如果在win8,win7情况下报错:未知错误(0x80005000) ----见https://www.jb51.net/article/72881.htm
using System; using System.Collections; using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace IISControlHelper { /// <summary> /// IIS 操作方法集合 /// https://www.jb51.net/article/72881.htm 错误 /// </summary> public class IISWorker { private 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", "ServerState", 2); site.Invoke("Put", "FrontPageWeb", 1); site.Invoke("Put", "DefaultDoc", "Default.html"); // site.Invoke("Put", "SecureBindings", ":443:"); 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"; if (isCreateAppPool) { DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools"); DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool"); newpool.Properties["AppPoolIdentityType"][0] = "4"; //4 newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式 newpool.CommitChanges(); siteVDir.Properties["AppPoolId"][0] = webSiteName; } 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.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; } } }
希望本文所述对大家的C#程序设计有所帮助。
上一篇: C#日历样式的下拉式计算器实例讲解
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25