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
///
/// iis 操作方法集合
/// 錯誤
///
public class iisworker
private static string hostname = "localhost";
///
/// 獲取本地iis版本
///
///
public static string getiisversion()
trydirectoryentry 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;
///
/// 建立虛擬目錄**
///
/// **名稱
/// 物理路徑
/// 站點+埠,如192.168.1.23:90
///
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)
// 建立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["path"][0] = physicalpath;
sitevdir.properties["accessflags"][0] = 513;
sitevdir.properties["frontpageweb"][0] = 1;
newpool.properties["managedpipelinemode"][0] = "0"; //0:整合模式 1:經典模式
newpool.commitchanges();
sitevdir.commitchanges();
site.commitchanges();
return siteid;
///
/// 得到**的物理路徑
///
/// **節點
///
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;
///
/// 獲取站點名
///
public static listgetserverbindings()
listiislist = new list();
string entpath = string.format("iis:///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;
iislist.add(iisinfo);
return iislist;
bool issucess = false;
try//建立乙個新程式池
directoryentry newpool;
//設定屬性 訪問使用者名稱和密碼 一般採取預設方式
newpool.properties["wamusername"][0] = username;
newpool.properties["wamuserpass"][0] = password;
newpool.commitchanges();
issucess = true;
return issucess;
catch // (exception ex)
return false;
///
/// 建立程式池後關聯相應應用程式及虛擬目錄
///
//獲取目錄
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)
//【測試成功通過】
getsite.commitchanges();
///
/// 判斷object物件是否為陣列
///
public static bool isarray(object o)
return o is array;
C 操作IIS伺服器Demo
using system using system.collections using system.collections.generic using system.directoryservices using system.linq using system.net using system....
C 操作IIS伺服器Demo
using system using system.collections using system.collections.generic using system.directoryservices using system.linq using system.net using system....
IIS伺服器配置
1.安裝iis服務,可以用3中方法同時發布兩個站點。1 不同的虛擬目錄分配 2 通過不同的埠分配 3 通過用不同的ip位址訪問 1 在預設的web站點新建虛擬目錄,例如乙個是caiwu目錄,乙個是renshi。在目錄的屬性中的文件選項新增index.html首頁。在ie中輸入,ip位址 caiwu或...