资料来源:网络整理
时间:2023/2/14 0:45:22 共计:3619 浏览
View Code
using System;
using System.Collections;
using System.DirectoryServices;
using Microsoft.Win32;
namespace SCL
{
public class IISHelper
{
static DirectoryEntry iisDE = new DirectoryEntry("IIS://localhost/W3SVC");
//判断 IIS是否安装
public static bool IsIISSetup
{
get
{
object obj=Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\Oc Manager\Subcomponents", "iis_inetmgr", "0");
if (obj == null) return false;
if(obj.ToString ().Equals("1"))
return true;
else
return false;
}
}
///<summary>
/// Get The Location IIS WebServers Information
///</summary>
///<returns></returns>
public static Hashtable GetLocationIIsWebServers()
{
Hashtable result = new Hashtable();
GetWebSiteInfo(ref result);
return result;
}
/// <summary>
/// 获取服务器IIS版本
/// </summary>
/// <param name="DomainName"></param>
/// <returns></returns>
public static WebServerTypes GetIISServerType(string DomainName)
{
if (string.IsNullOrEmpty(DomainName))
{
DomainName = "LOCALHOST";
}
string path = "IIS://" + DomainName + "/W3SVC/INFO";
DirectoryEntry entry = null;
try
{
entry = new DirectoryEntry(path);
}
catch
{
return WebServerTypes.Unknown;
}
int num = 5;
try
{
num = (int)entry.Properties["MajorIISVersionNumber"].Value;
}
catch
{
return WebServerTypes.IIS5;
}
switch (num)
{
case 6:
return WebServerTypes.IIS6;
case 7:
return WebServerTypes.IIS7;
}
return WebServerTypes.IIS6;
}
///<summary>
/// Create a new IIS Webserver
///</summary>
///<param name="webServerName">webserver name</param>
///<param name="path">webserver directory path</param>
///<param name="port">access port</param>
public static void CreateNewIIsWebServer(string webServerName, string path, int port)
{
int siteID = GetWebSiteInfo(port);
using (DirectoryEntry site = (DirectoryEntry)iisDE.Invoke("Create", "IIsWebServer", siteID))
{
site.Invoke("Put", "ServerComment", webServerName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", ":" + port.ToString() + ":");
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", "Default.aspx");
site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
using (DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir"))
{
siteVDir.Properties["AppIsolated"][0] = 2;
siteVDir.Properties["Path"][0] = path;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["FrontPageWeb"][0] = 1;
//siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/"+webServerName;
siteVDir.Properties["AppFriendlyName"][0] = webServerName;
siteVDir.Invoke("AppCreate", true);
siteVDir.CommitChanges();
}
site.CommitChanges();
}
}
///<summary>
/// Create a new virtual directory
///</summary>
///<param name="website">webserver name</param>
///<param name="dirName">virtual directory name</param>
///<param name="properties">virtual dirctory properties</param>
public static void CreateNewVirtualDir(string website, string dirName, System.Data.PropertyCollection properties)
{
if (GetVirtualDir(website, dirName)) throw new Exception(" The Virtual Dir is alread existed");
using (DirectoryEntry de = GetWebSiteInfo(website))
{
using (DirectoryEntry vde = de.Children.Add(dirName, "IIsWebVirtualDir"))
{
vde.CommitChanges();
de.CommitChanges();
UpdateVDirInfo(vde, ref properties);
vde.Invoke("AppCreate", true);
vde.CommitChanges();
de.CommitChanges();
}
}
}
///<summary>
/// Get Common virtual directory setting
///</summary>
///<param name="path">virtual directory path</param>
///<param name="vdir">virtual directory name</param>
///<returns></returns>
public static System.Data.PropertyCollection GetDefaultVirtualDirSetting(string path, string vdir)
{
System.Data.PropertyCollection vProperty = new System.Data.PropertyCollection();
//vProperty.Add("AnonymousUserName","");
//vProperty.Add("AnonymousUserPass","");
vProperty.Add("AccessRead", true);
vProperty.Add("AccessExecute", false);
vProperty.Add("AuthBasic", true);
vProperty.Add("AuthNTLM", true);
vProperty.Add("ContentIndexed", true);
vProperty.Add("EnableDefaultDoc", true);
vProperty.Add("EnableDirBrowsing", false);
vProperty.Add("AccessSSL", false);
vProperty.Add("AccessScript", false);
vProperty.Add("DefaultDoc", "Default.aspx");
vProperty.Add("Path", path);
vProperty.Add("AppIsolated", 2);
vProperty.Add("AppFriendlyName", vdir);
vProperty.Add("AccessFlags", 513);
vProperty.Add("FrontPageWeb", 1);
//vProperty.Add("DontLog", true);
//vProperty.Add("AppRoot", "LM/W3SVC/" + siteID + "/" + vdir);
return vProperty;
}
///<summary>
/// Delete the virtual directory in the WebServer
///</summary>
///<param name="website">webserver name</param>
///<param name="vdir">virtual directory name</param>
public static void DeleteVirtualDir(string website, string vdir)
{
if (!GetVirtualDir(website, vdir)) throw new Exception(" The virtual directory don't exist in the website");
using (DirectoryEntry de = GetWebSiteInfo(website))
{
foreach (DirectoryEntry sub in de.Children)
{
if (sub.Name == vdir)
{
de.Invoke("Delete", new string[] { sub.SchemaClassName, vdir });
de.CommitChanges();
}
}
}
}
private static void UpdateVDirInfo(DirectoryEntry newDE, ref System.Data.PropertyCollection properties)
{
//newDE.Properties["AnonyMousUserName"][0] = properties["AnonymousUserName"].ToString();
//newDE.Properties["AnonymousUserPass"][0] = properties["AnonymousUserPass"].ToString();
newDE.Properties["AccessRead"][0] = (bool)properties["AccessRead"];
newDE.Properties["AccessExecute"][0] = (bool)properties["AccessExecute"];
newDE.Properties["AuthBasic"][0] = (bool)properties["AuthBasic"];
newDE.Properties["AuthNTLM"][0] = (bool)properties["AuthNTLM"];
newDE.Properties["ContentIndexed"][0] = (bool)properties["ContentIndexed"];
newDE.Properties["EnableDefaultDoc"][0] = (bool)properties["EnableDefaultDoc"];
newDE.Properties["EnableDirBrowsing"][0] = (bool)properties["EnableDirBrowsing"];
newDE.Properties["AccessSSL"][0] = (bool)properties["AccessSSL"];
newDE.Properties["AccessScript"][0] = (bool)properties["AccessScript"];
newDE.Properties["DefaultDoc"][0] = properties["DefaultDoc"].ToString();
newDE.Properties["Path"][0] = properties["Path"].ToString();
newDE.Properties["AppIsolated"][0] = (int)properties["AppIsolated"];
newDE.Properties["AppFriendlyName"][0] = properties["AppFriendlyName"].ToString();
newDE.Properties["AccessFlags"][0] = (int)properties["AccessFlags"];
newDE.Properties["FrontPageWeb"][0] = (int)properties["FrontPageWeb"];
//newDE.Properties["DontLog"][0] = (bool)properties["DontLog"];
//newDE.Properties["AppRoot"][0] = properties["AppRoot"].ToString();
}
public static bool GetVirtualDir(string webSite, string dirName)
{
bool result = false;
using (DirectoryEntry de = GetWebSiteInfo(webSite))
{
if (de != null)
{
foreach (DirectoryEntry subVD in de.Children)
{
if (subVD.SchemaClassName == "IIsWebVirtualDir" && subVD.Name == dirName)
{
result = true;
break;
}
}
}
}
return result;
}
private static void GetWebSiteInfo(ref Hashtable webServer)
{
DirectoryEntries des = iisDE.Children;
foreach (DirectoryEntry subDE in des)
{
if (subDE.SchemaClassName == "IIsWebServer")
{
webServer.Add(subDE.Properties["ServerComment"].Value.ToString(), subDE.Name);
}
}
des = null;
}
private static DirectoryEntry GetWebSiteInfo(string website)
{
DirectoryEntry result = null;
DirectoryEntries des = iisDE.Children;
foreach (DirectoryEntry subDE in des)
{
if (subDE.SchemaClassName == "IIsWebServer" && subDE.Properties["ServerComment"].Value.ToString() == website)
{
result = subDE.Children.Find("Root", "IIsWebVirtualDir");
break;
}
}
des = null;
return result;
}
private static int GetWebSiteInfo(int port)
{
int result = 1, i = 1;
DirectoryEntries des = iisDE.Children;
foreach (DirectoryEntry subDE in des)
{
if (subDE.SchemaClassName == "IIsWebServer")
{
if ((i = Convert.ToInt32(subDE.Name)) >= result)
{
result = i + 1;
}
if (subDE.Properties["ServerBindings"][0].ToString() == ":" + port.ToString() + ":")
{
throw new Exception(" The port is already used");
}
}
}
des = null;
return result;
}
}
public enum WebServerTypes
{
Unknown,
IIS5,
IIS6,
IIS7
}
}
/// <summary>
/// 獲取VDir支持的所有屬性
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> GetVDirPropertyName()
{
//System.DirectoryServices.DirectoryEntry VDir;
/* IIS://localhost/W3SVC/1"有如下属性
LogFileDirectory=C:\inetpub\logs\LogFiles
LogPluginClsid={FF160663-DE82-11CF-BC0A-00AA006111E0}
AppPoolId=ASP.NET v4.0
DontLog=False
TraceUriPrefix=没有值
DirBrowseFlags=1073741854
DefaultDoc=Default.htm,Default.asp,index.htm,index.html,iisstart.htm,default.aspx
AuthFlags=7
AnonymousUserName=IUSR
AspDiskTemplateCacheDirectory=C:\inetpub\temp\ASP Compiled Templates
HttpErrors=System.Object[]
SSIExecDisable=False
AccessSSLFlags=0
NTAuthenticationProviders=Negotiate,NTLM
AccessFlags=513
ScriptMaps=System.Object[]
RedirectHeaders=没有值
HttpCustomHeaders=X-Powered-By: ASP.NET
ServerState=2
Win32Error=0
ServerCommand=1
KeyType=IIsWebServer
ServerAutoStart=True
ServerComment=Default Web Site
ServerBindings=:80:localhost.huawei.com
CreateCGIWithNewConsole=True
**/
/* IIS://localhost/W3SVC/1/ROOT" 有如下属性
AppPoolId=DefaultAppPool
DontLog=False
DirBrowseFlags=1073741854
DefaultDoc=Default.htm,Default.asp,index.htm,index.html,iisstart.htm,default.aspx
AuthFlags=7
AnonymousUserName=IUSR
AspDiskTemplateCacheDirectory=C:\inetpub\temp\ASP Compiled Templates
HttpErrors=System.Object[]
SSIExecDisable=False
AccessSSLFlags=0
NTAuthenticationProviders=Negotiate,NTLM
AccessFlags=513
ScriptMaps=System.Object[]
RedirectHeaders=没有值
HttpCustomHeaders=X-Powered-By: ASP.NET
CreateCGIWithNewConsole=True
KeyType=IIsWebVirtualDir
Path=C:\inetpub\wwwroot
UNCUserName=china\xWX70375
UNCPassword=Huaci188
AppIsolated=0
AppRoot=/LM/W3SVC/1/ROOT
**/
try
{
const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1";//IIS://ComputerName/Service/WebsiteId/Directory
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
string sOut = "";
//下面的方法是得到所有属性名称的方法:
Dictionary<string, string> keyValueDic = new Dictionary<string, string>();
foreach (PropertyValueCollection pvc in root.Properties)
{
//Console.WriteLine(pvc.PropertyName);
if (pvc.Value == null)
{
keyValueDic.Add(pvc.PropertyName, "没有值");
}
else
{
keyValueDic.Add(pvc.PropertyName, pvc.Value.ToString());
//sOut += pvc.PropertyName + "=" +pvc.Value.ToString() + System.Environment.NewLine;
}
}
return keyValueDic;
}
catch (Exception ex)
{
//服务帐户配置中不存在服务正常运行所需的特权。
//您可以使用 Services Microsoft Management Console (MMC)管理单元(services.msc)
//和 Local Security Settings MMC 管理单元(secpol.msc)来查看服务配置和帐户配置
//解决方法,将Services.msc中IIS Admin Service设为本地系统账户登录
return null;
}
}

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。