C#操作IIS方法集合
C# 操作IIS方法集合
如果在win8,win7情況下報(bào)錯(cuò):未知錯(cuò)誤(0x80005000) ----見(jiàn)//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 操作方法集合 /// //www.jb51.net/article/72881.htm 錯(cuò)誤 /// </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) { //說(shuō)明一點(diǎn):IIS5.0中沒(méi)有(int)entry.Properties["MajorIISVersionNumber"].Value;屬性,將拋出異常 證明版本為 5.0 return string.Empty; } } /// <summary> /// 創(chuàng)建虛擬目錄網(wǎng)站 /// </summary> /// <param name="webSiteName">網(wǎng)站名稱</param> /// <param name="physicalPath">物理路徑</param> /// <param name="domainPort">站點(diǎn)+端口,如192.168.1.23:90</param> /// <param name="isCreateAppPool">是否創(chuàng)建新的應(yīng)用程序池</param> /// <returns></returns> public static int CreateWebSite(string webSiteName, string physicalPath, string domainPort,bool isCreateAppPool) { DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC"); // 為新WEB站點(diǎn)查找一個(gè)未使用的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; } } } // 創(chuàng)建WEB站點(diǎn) 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"); // 創(chuàng)建應(yīng)用程序虛擬目錄 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:經(jīng)典模式 newpool.CommitChanges(); siteVDir.Properties["AppPoolId"][0] = webSiteName; } siteVDir.CommitChanges(); site.CommitChanges(); return siteID; } /// <summary> /// 得到網(wǎng)站的物理路徑 /// </summary> /// <param name="rootEntry">網(wǎng)站節(jié)點(diǎn)</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> /// 獲取站點(diǎn)名 /// </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))//如果有多個(gè)綁定站點(diǎn)時(shí) { object[] objectToArr = (object[])objectArr; serverBindingStr = objectToArr[0].ToString(); } else//只有一個(gè)綁定站點(diǎn) { serverBindingStr = child.Properties["ServerBindings"].Value.ToString(); } IISInfo iisInfo = new IISInfo(); iisInfo.DomainPort = serverBindingStr; iisInfo.AppPool = child.Properties["AppPoolId"].Value.ToString();//應(yīng)用程序池 iisList.Add(iisInfo); } } } return iisList; } public static bool CreateAppPool(string appPoolName, string Username, string Password) { bool issucess = false; try { //創(chuàng)建一個(gè)新程序池 DirectoryEntry newpool; DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools"); newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool"); //設(shè)置屬性 訪問(wèn)用戶名和密碼 一般采取默認(rèn)方式 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> /// 建立程序池后關(guān)聯(lián)相應(yīng)應(yīng)用程序及虛擬目錄 /// </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")) { //設(shè)置應(yīng)用程序程序池 先獲得應(yīng)用程序 在設(shè)定應(yīng)用程序程序池 //第一次測(cè)試根目錄 foreach (DirectoryEntry getchild in getentity.Children) { if (getchild.SchemaClassName.Equals("IIsWebVirtualDir")) { //找到指定的虛擬目錄. foreach (DirectoryEntry getsite in getchild.Children) { if (getsite.Name.Equals(appname)) { //【測(cè)試成功通過(guò)】 getsite.Properties["AppPoolId"].Value = poolName; getsite.CommitChanges(); } } } } } } } /// <summary> /// 判斷object對(duì)象是否為數(shù)組 /// </summary> public static bool IsArray(object o) { return o is Array; } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
上一篇:C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)獲取IIS站點(diǎn)及虛擬目錄信息的方法
本文標(biāo)題:C#操作IIS方法集合
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6932.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)