如何为特定网站获取应用程序池名称IIS 6的方案使用C#如何以编程方式获取特定网站IIS6的应用程序池名称? C#
编辑: 我已经使用的DirectoryServices命名空间,但应用程序池名称的方法,除非它被明确设置不正确检索使用相同的代码。这意味着如果您使用iis管理器手动添加网站并设置应用程序池,那么当我使用sharepoint创建应用程序并设置不同的appPool这些方法不工作时,这些代码将无法工作(它将始终返回DefaultAppPool)。
如何为特定网站获取应用程序池名称IIS 6的方案使用C#如何以编程方式获取特定网站IIS6的应用程序池名称? C#
编辑: 我已经使用的DirectoryServices命名空间,但应用程序池名称的方法,除非它被明确设置不正确检索使用相同的代码。这意味着如果您使用iis管理器手动添加网站并设置应用程序池,那么当我使用sharepoint创建应用程序并设置不同的appPool这些方法不工作时,这些代码将无法工作(它将始终返回DefaultAppPool)。
System.DirectoryServices namespace中的课程将帮助您获取该信息。
检查this article by Rick Strahl为例:
/// <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;
}
总之,有这样做的春天在脑海中的2种方式。
的不太成熟的方法是知道,IIS6的设置存储在其中只是一个XML文件的元数据库:
C:\WINDOWS\system32\inetsrv\MetaBase.xml
你可以只使用Linq2Xml和解析XML寻找网站名称或ID,该AppPoolId属性包含的应用程序池
正确的方法名是使用的System.DirectoryServices
我不同意你的看法。我编写了一个测试应用程序,并从中得到正确的AppPool名称,即使我使用IIS管理器手动设置AppPool。
为了确定,我测试过一次,名字没问题;然后,我打开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;
}
}
对不起,我糟糕的英语。
希望我帮了忙。
我已经使用这些方法,但应用程序池名称不能正确检索,除非通过使用相同的代码明确设置。 这意味着如果您使用iis管理器手动添加网站并设置应用程序池,那么当我使用sharepoint创建应用程序并设置不同的appPool时,这些代码将无法工作(它总是会返回DefaultAppPool) – 2009-02-04 13:30:21
dont工作。 – 2009-02-04 13:30:58