我用 C# 编写了代码来启动和停止 IIS 池。我使用了 ServerManager 方法并尝试使用 ServerManager().ApplicationPools 但它只显示以下五个应用程序池,而不是我在 IIS 中的池。我的 IIS 版本是 10.0.19041.1 。下面是提到的代码。
我得到的 IIS 池
“Clr4集成应用程序池” “Clr4经典应用程序池” “Clr2集成应用程序池” “Clr2经典应用程序池” “非托管经典应用程序池”
private void PerformAction(string action)
{
// Get selected server and app pools
string selectedServer = ddlServers.SelectedValue;
List<string> selectedAppPools = new List<string>();
foreach (ListItem item in cblAppPools.Items)
{
if (item.Selected)
{
selectedAppPools.Add(item.Value);
}
}
// Perform start or stop action on selected app pools
foreach (string appPool in selectedAppPools)
{
try
{
using (ServerManager serverManager = new ServerManager())
{
Console.WriteLine($"Trying to access App Pool: {appPool}");
ApplicationPool selectedAppPool = serverManager.ApplicationPools[appPool];
if (action.Equals("start", StringComparison.OrdinalIgnoreCase))
{
selectedAppPool.Start();
}
else if (action.Equals("stop", StringComparison.OrdinalIgnoreCase))
{
selectedAppPool.Stop();
}
}
}
catch (Exception ex)
{
// Handle exception (log it, display it to the user, etc.)
}
}
// Refresh the UI or display a message to the user
LoadAppPools(selectedServer);
}
}
您可以使用此代码作为参考:
// Writes out the applications and the application pool names
// associated with the applications under the default Web site.
public void GetApplicationPoolNames()
{
ServerManager manager = new ServerManager();
Site defaultSite = manager.Sites["Default Web Site"];
foreach (Application app in defaultSite.Applications)
{
Console.WriteLine(
"{0} is assigned to the '{1}' application pool.",
app.Path, app.ApplicationPoolName);
}
}