C# 如何以编程方式获取特定网站 IIS6 的应用程序池名称?C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/511263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the application pool name for a specific website IIS6 programmatically? C#
提问by Amr Elsehemy
how to get the application pool name for a specific website IIS 6 programmatic using C#
如何使用 C# 以编程方式获取特定网站的应用程序池名称 IIS 6
EDIT: I already used the methods of DirectoryServices namespace but the application pool name isn't retrieved correctly unless it was explicitly set by using the same code. Which means if u add a website manually using the iis manager and set an application pool, those codes won't work (it will always return DefaultAppPool) more over when I create an application using sharepoint and set a different appPool those methods dont work.
编辑:我已经使用了 DirectoryServices 命名空间的方法,但除非使用相同的代码显式设置,否则无法正确检索应用程序池名称。这意味着如果您使用 iis 管理器手动添加网站并设置应用程序池,那么当我使用 sharepoint 创建应用程序并设置不同的 appPool 时,这些代码将不起作用(它将始终返回 DefaultAppPool),这些方法不起作用。
采纳答案by M4N
The classes in the System.DirectoryServices namespacewill help you get that information.
System.DirectoryServices 命名空间中的类将帮助您获取该信息。
Check this article by Rick Strahlfor an example:
检查本文由Rick施特拉尔为例:
/// <summary>
/// Returns a list of all the Application Pools configured
/// </summary>
/// <returns></returns>
public ApplicationPool[] GetApplicationPools()
{
if (ServerType != WebServerTypes.IIS6 && ServerType != WebServerTypes.IIS7)
return null;
DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");
if (root == null)
return null;
List<ApplicationPool> Pools = new List<ApplicationPool>();
foreach (DirectoryEntry Entry in root.Children)
{
PropertyCollection Properties = Entry.Properties;
ApplicationPool Pool = new ApplicationPool();
Pool.Name = Entry.Name;
Pools.Add(Pool);
}
return Pools.ToArray();
}
/// <summary>
/// Create a new Application Pool and return an instance of the entry
/// </summary>
/// <param name="AppPoolName"></param>
/// <returns></returns>
public DirectoryEntry CreateApplicationPool(string AppPoolName)
{
if (this.ServerType != WebServerTypes.IIS6 && this.ServerType != WebServerTypes.IIS7)
return null;
DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools");
if (root == null)
return null;
DirectoryEntry AppPool = root.Invoke("Create", "IIsApplicationPool", AppPoolName) as DirectoryEntry;
AppPool.CommitChanges();
return AppPool;
}
/// <summary>
/// Returns an instance of an Application Pool
/// </summary>
/// <param name="AppPoolName"></param>
/// <returns></returns>
public DirectoryEntry GetApplicationPool(string AppPoolName)
{
DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools/" + AppPoolName);
return root;
}
/// <summary>
/// Retrieves an Adsi Node by its path. Abstracted for error handling
/// </summary>
/// <param name="Path">the ADSI path to retrieve: IIS://localhost/w3svc/root</param>
/// <returns>node or null</returns>
private DirectoryEntry GetDirectoryEntry(string Path)
{
DirectoryEntry root = null;
try
{
root = new DirectoryEntry(Path);
}
catch
{
this.SetError("Couldn't access node");
return null;
}
if (root == null)
{
this.SetError("Couldn't access node");
return null;
}
return root;
}
回答by danswain
In brief, there's 2 ways of doing this that spring to mind.
简而言之,有两种方法可以做到这一点。
The less sophisticated way is knowing that, IIS6's settings are stored in the MetaBase which is just an Xml file:
不太复杂的方法是知道 IIS6 的设置存储在 MetaBase 中,它只是一个 Xml 文件:
C:\WINDOWS\system32\inetsrv\MetaBase.xml
You can just use Linq2Xml and parse the Xml looking for the sites name or Id, The AppPoolId attribute contains the name of the AppPool
您可以只使用 Linq2Xml 并解析 Xml 以查找站点名称或 Id,AppPoolId 属性包含 AppPool 的名称
The proper way is to use System.DirectoryServices
正确的方法是使用 System.DirectoryServices
回答by Ricardo Nolde
I don't agree with you. I coded up a test app and I get the correct AppPool name from it, even if I set the AppPool manually using IIS Manager.
我不同意你的看法。我编写了一个测试应用程序并从中获得了正确的 AppPool 名称,即使我使用 IIS 管理器手动设置了 AppPool。
To make sure, I have tested once, name name was ok; then, I popep up the IIS Manager, changed the AppPool, executed iisreset
, and ran the test app again - the AppPool name I got was correct again. I don't how your code looked like, but mine is like this:
为了确保,我已经测试过一次,name name 没问题;然后,我弹出 IIS 管理器,更改 AppPool,执行iisreset
并再次运行测试应用程序 - 我得到的 AppPool 名称再次正确。我不知道你的代码是什么样子的,但我的是这样的:
using System;
using System.IO;
using System.DirectoryServices;
class Class
{
static void Main(string[] args)
{
DirectoryEntry entry = FindVirtualDirectory("<Server>", "Default Web Site", "<WantedVirtualDir>");
if (entry != null)
{
Console.WriteLine(entry.Properties["AppPoolId"].Value);
}
}
static DirectoryEntry FindVirtualDirectory(string server, string website, string virtualdir)
{
DirectoryEntry siteEntry = null;
DirectoryEntry rootEntry = null;
try
{
siteEntry = FindWebSite(server, website);
if (siteEntry == null)
{
return null;
}
rootEntry = siteEntry.Children.Find("ROOT", "IIsWebVirtualDir");
if (rootEntry == null)
{
return null;
}
return rootEntry.Children.Find(virtualdir, "IIsWebVirtualDir");
}
catch (DirectoryNotFoundException ex)
{
return null;
}
finally
{
if (siteEntry != null) siteEntry.Dispose();
if (rootEntry != null) rootEntry.Dispose();
}
}
static DirectoryEntry FindWebSite(string server, string friendlyName)
{
string path = String.Format("IIS://{0}/W3SVC", server);
using (DirectoryEntry w3svc = new DirectoryEntry(path))
{
foreach (DirectoryEntry entry in w3svc.Children)
{
if (entry.SchemaClassName == "IIsWebServer" &&
entry.Properties["ServerComment"].Value.Equals(friendlyName))
{
return entry;
}
}
}
return null;
}
}
Sorry for my lousy english.
Hope I've helped.
对不起我糟糕的英语。
希望我有所帮助。