C#操作IIS辦法聚集。本站提示廣大學習愛好者:(C#操作IIS辦法聚集)文章只能為提供參考,不一定能成為您想要的結果。以下是C#操作IIS辦法聚集正文
C# 操作IIS辦法聚集
假如在win8,win7情形下報錯:未知毛病(0x80005000) ----見http://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 操作辦法聚集
/// http://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#法式設計有所贊助。