使用 ServerManager 类是否可以从应用程序的物理路径中获取应用程序池名称?我一直在使用以下代码进行测试:
using (var manager = new ServerManager())
//using (var manager = ServerManager.OpenRemote("xxx")) //Uncomment this line to enable remote debugging, comment out line 1821, update Server Name value
{
//Get Site using Website ID
var site = manager.Sites.SingleOrDefault(m => m.Id == webSiteID);
if (site != null)
{
//foreach (Application app in site.Applications)
//{
bld.AppendLine("1. Stopping App Pool for: " + site.Name);
//Get the application pool name
var application = site.Applications.SingleOrDefault(m => m.VirtualDirectories["/"].PhysicalPath == @"D:/inetpub/TestSite1"); //RETURNS NULL
//Get the application binded to the siteName
var application1 = site.Applications.SingleOrDefault(m => m.Path == "/TestSite1"); //RETURN INFO I REQUIRE
var appPoolName = application.ApplicationPoolName;
//var appPoolName = site.Applications.FirstOrDefault().ApplicationPoolName;
var appPool = manager.ApplicationPools[appPoolName];
if (appPool != null)
{
//Check the App Pool State
if (appPool.State == ObjectState.Stopped)
{
//App Pool already stopped
bld.AppendLine("1. App Pool: " + appPool.Name + " already stopped.");
}
else
{
//Stop the App Pool
appPool.Stop();
bld.AppendLine("1. App Pool: " + appPool.Name + " stopped.");
}
}
else
{
bld.AppendLine("1. No App Pool found.");
failFlag = true;
}
//}
}
else
{
bld.AppendLine("1. SiteID: " + webSiteID + " does not exist.");
failFlag = true;
}
}
}
我想使用物理路径而不是虚拟路径来获取应用程序,因为我的应用程序数据库中有数百个应用程序,而且我不能 100% 认为它们都已在 IIS 中配置,两条路径都在同步例如 D:\inetpub\TestSite1 和 /TestSite1