///
/// 获取本地IIS版本
///
///
public string GetIIsVersion()
{
try
{
DirectoryEntry entry = new
DirectoryEntry("IIS://localhost/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
/// 是否创建新的应用程序池 ///
public int CreateWebSite(string webSiteName, string physicalPath, string domainPort, bool isCreateAppPool)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
// 为新WEB站点查找一个未使用的ID
int siteID = 1;
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(https://www.doczj.com/doc/cf1717771.html,);
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", "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://localhost/W3SVC/AppPools");
DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");
newpool.Properties["AppPoolIdentityType"][0] = "4"; //4
newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式1:经典模式
https://www.doczj.com/doc/cf1717771.html,mitChanges();
siteVDir.Properties["AppPoolId"][0] = webSiteName;
}
https://www.doczj.com/doc/cf1717771.html,mitChanges();
https://www.doczj.com/doc/cf1717771.html,mitChanges();
return siteID;
}
///
/// 得到网站的物理路径
///
/// 网站节点
///
public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry) {
string physicalPath = "";
foreach (DirectoryEntry childEntry in rootEntry.Children)
{
if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (https://www.doczj.com/doc/cf1717771.html,.ToLower() == "root"))
{
if (childEntry.Properties["Path"].Value != null)
{
physicalPath = childEntry.Properties["Path"].Value.ToString();
}
else
{
physicalPath = "";
}
}
}
return physicalPath;
}
///
/// 获取站点列表
///
public static List
{
List
string entPath = String.Format("IIS://localhost/w3svc"); 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.siteNum = https://www.doczj.com/doc/cf1717771.html,;//站点编号
iisInfo.SiteName =
child.Properties["ServerComment"].Value.ToString();//站点名称iisInfo.DomainPort = serverBindingStr;//绑定
iisInfo.AppPool = child.Children.Find("Root", "IISWebVirtualDir").Properties["AppPoolId"].Value.ToString();//应用程序池名称
DirectoryEntry siteEntry = new
DirectoryEntry(String.Format("IIS://localhost/w3svc/{0}",
https://www.doczj.com/doc/cf1717771.html,));
iisInfo.SitePath = GetWebsitePhysicalPath(siteEntry);//获取站点路径iisList.Add(iisInfo);
}
}
}
return iisList;
}
public static string GetServerBindingsString()//返回拼接字符串
{
string result = "";
string entPath = String.Format("IIS://localhost/w3svc"); 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(); }
DirectoryEntry siteEntry = new
DirectoryEntry(String.Format("IIS://localhost/w3svc/{0}",
https://www.doczj.com/doc/cf1717771.html,));
result += https://www.doczj.com/doc/cf1717771.html, + "," +
child.Properties["ServerComment"].Value.ToString() + "," + serverBindingStr + "," + child.Children.Find("Root", "IISWebVirtualDir").Properties["AppPoolId"].Value.ToString() + "," + GetWebsitePhysicalPath(siteEntry) + ";";
}
}
}
return result;
}
///
/// 创建应用池
///
///
///
///
///
public bool CreateAppPool(string appPoolName, string Username, string Password)
{
bool issucess = false;
try
{
//创建一个新程序池
DirectoryEntry newpool;
DirectoryEntry apppools = new
DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool"); //设置属性访问用户名和密码一般采取默认方式
newpool.Properties["WAMUserName"][0] = Username;
newpool.Properties["WAMUserPass"][0] = Password;
newpool.Properties["AppPoolIdentityType"][0] = "3";
https://www.doczj.com/doc/cf1717771.html,mitChanges();
issucess = true;
return issucess;
}
catch // (Exception ex)
{
return false;
}
}
public void CreateAppPool(string AppPoolName)
{
if (!IsAppPoolName(AppPoolName))
{
DirectoryEntry newpool;
DirectoryEntry appPools = new
DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool"); https://www.doczj.com/doc/cf1717771.html,mitChanges();
MessageBox.Show(AppPoolName + "程序池增加成功");
}
////修改应用程序的配置(包含托管模式及其NET运行版本)
//ServerManager sm = new ServerManager();
//sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0"; //sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成Classic为经典//https://www.doczj.com/doc/cf1717771.html,mitChanges();
//MessageBox.Show(AppPoolName + "程序池托管管道模式:" +
sm.ApplicationPools[AppPoolName].ManagedPipelineMode.ToString() + "运行的NET版本为:" +
sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion);
}
///
/// 判断程序池是否存在
///
/// 程序池名称
///
private bool IsAppPoolName(string AppPoolName)
{
bool result = false;
DirectoryEntry appPools = new
DirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach (DirectoryEntry getdir in appPools.Children)
{
if (https://www.doczj.com/doc/cf1717771.html,.Equals(AppPoolName))
{
result = true;
}
}
return result;
}
///
/// 获取应用池列表
///
///
public string AppPoolNameList()
{
string list = "";
DirectoryEntry appPools = new
DirectoryEntry("IIS://localhost/W3SVC/AppPools");
foreach (DirectoryEntry getdir in appPools.Children)
{
list += https://www.doczj.com/doc/cf1717771.html, + ",";//应用池名称
}
if (list != "")
{
list = list.Substring(0, list.Length - 1);
}
return list;
}
///
/// 根据站点名称查询对应应用池名称
///
///
///
public string AppPoolNameBySiteName(string siteName)
{
string AppPoolName = "";
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC"); foreach (DirectoryEntry website in root.Children)
{
if (website.SchemaClassName != "IIsWebServer") continue;
string comment = website.Properties["ServerComment"][0].ToString(); if (comment == siteName)
{
DirectoryEntry siteVDir = website.Children.Find("Root", "IISWebVirtualDir");
AppPoolName = siteVDir.Properties["AppPoolId"][0].ToString();
return AppPoolName;
}
}
return string.Empty;
}
///
/// 建立程序池后关联相应应用程序及虚拟目录
///
public 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 (https://www.doczj.com/doc/cf1717771.html,.Equals(appname))
{
//【测试成功通过】
getsite.Properties["AppPoolId"].Value = poolName;
https://www.doczj.com/doc/cf1717771.html,mitChanges();
}
}
}
}
}
}
}
///
/// 已知路径删除站点目录文件
///
/// 网站路径
public string DeleteSiteFile(string sitepath) //void DeleteSiteFile(string
siteName)
{
try
{
Directory.Delete(sitepath, true);
return "删除站点成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
///
/// 根据站点名称删除站点目录文件
///
/// 网站路径
public string DeleteSiteFileBySiteName(string siteName)
{
try
{
string siteNum = GetWebSiteNum(siteName);
DirectoryEntry siteEntry = new
DirectoryEntry(String.Format("IIS://localhost/w3svc/{0}", siteNum)); string sitePath = GetWebsitePhysicalPath(siteEntry);//获取站点路径DirectoryInfo dir = new DirectoryInfo(sitePath);
if (dir.Exists)
{
Directory.Delete(sitePath, true);
}
return "删除站点成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
///
/// 获取网站编号
///
///
///
public string GetWebSiteNum(string siteName)
{
Regex regex = new Regex(siteName);
string tmpStr;
DirectoryEntry ent = new DirectoryEntry("IIS://localhost/w3svc"); foreach (DirectoryEntry child in ent.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerBindings"].Value != null)
{
tmpStr = child.Properties["ServerBindings"].Value.ToString();
if (regex.Match(tmpStr).Success)
{
return https://www.doczj.com/doc/cf1717771.html,;
}
}
if (child.Properties["ServerComment"].Value != null)
{
tmpStr = child.Properties["ServerComment"].Value.ToString();
if (regex.Match(tmpStr).Success)
{
return https://www.doczj.com/doc/cf1717771.html,;
}
}
}
}
throw new Exception("没有找到我们想要的站点" + siteName);
}
///
/// 获取网站编号
///
private string getWebSiteNum(string siteName)
{
using (DirectoryEntry ent = new
DirectoryEntry("IIS://localhost/w3svc"))
{
foreach (DirectoryEntry child in ent.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerComment"].Value != null)
{
string tmpStr = child.Properties["ServerComment"].Value.ToString(); if (tmpStr.Equals(siteName))
{
return https://www.doczj.com/doc/cf1717771.html,;
}
}
}
}
}
return string.Empty;
}
static Hashtable hs = new Hashtable();//创建哈希表,保存池中的站点static string[] pls;//池数组
static string[] nums;//应用程序池中各自包含的网站数量
///
/// 判断网站名与应用程序池名称是否相等
///
/// 网站名称
///
public bool chname(string wnames)
{
bool ctf = true;
pls = AppPoolNameList().Split(',');
foreach (string i in pls)
{
if (wnames == i)
ctf = false;
else ctf = true;
}
return ctf;
}
///
/// 获得池数组对应的网站数量
///
public void WebNums()
{
List
pls = AppPoolNameList().Split(',');
foreach (string i in pls)
{
if (hs[i].ToString() != "")
weblist.Add(hs[i].ToString().Split(',').Length.ToString()); else
weblist.Add("0");
}
nums = weblist.ToArray();
}
///
/// 移动网站到新池
///
/// 网站名称
/// 旧池名称
/// 新池名称
public void movepool(string webns, string poolold, string poolns)
{
pls = AppPoolNameList().Split(',');
try
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC"); foreach (DirectoryEntry website in root.Children)
{
if (website.SchemaClassName != "IIsWebServer") continue;
string comment = website.Properties["ServerComment"][0].ToString(); if (comment == webns)
{
DirectoryEntry siteVDir = website.Children.Find("Root", "IISWebVirtualDir");
siteVDir.Invoke("Put", new object[2] { "AppPoolId", poolns }); https://www.doczj.com/doc/cf1717771.html,mitChanges();
website.Invoke("Put", new object[2] { "AppPoolId", poolns }); https://www.doczj.com/doc/cf1717771.html,mitChanges();
}
}
for (int i = 0; i < pls.Length; i++)//遍历旧池并修改原数目数组的数据
{
if (pls[i] == poolold)
{
nums[i] = (int.Parse(nums[i]) - 1).ToString();
string[] h = hs[poolold].ToString().Split(',');
string hnew = "";
foreach (string s in h)
if (s != webns)
{
if (hnew == "")
hnew = s;
else hnew += "," + s;
}
hs[poolold] = hnew;
if (hs[poolns].ToString() == "") hs[poolns] = webns;
else hs[poolns] += "," + webns;
}
if (pls[i] == poolns)
{
WebNums();
nums[i] = (int.Parse(nums[i]) + 1).ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
///
/// 应用池操作
///
/// 应用池名称
/// Start开启Recycle回收Stop 停止
///
public string AppPoolOP(string AppPoolName, string state) {
try
{
DirectoryEntry appPool = new
DirectoryEntry("IIS://localhost/W3SVC/AppPools"); DirectoryEntry findPool = appPool.Children.Find(AppPoolName, "IIsApplicationPool");
findPool.Invoke(state, null);
https://www.doczj.com/doc/cf1717771.html,mitChanges();
appPool.Close();
return "操作成功";
}
catch (Exception ex)
{
return "操作失败";
}
}
///
/// 判断object对象是否为数组
///
public static bool IsArray(object o) {
return o is Array;
}