ConfigurationElementCollection을 사용하여 web.config를 검색하려고합니다. 내가 그것을 추가하기 전에 해당 요소가 이미 존재하는지 어떻게 확인합니까추가하기 전에 요소가 이미 있는지 확인하려면 어떻게해야합니까?
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetWebConfiguration("Default Web Site/app1");
ConfigurationSection modulesSection = config.GetSection("system.webServer/modules");
ConfigurationElementCollection modulesCollection = modulesSection.GetCollection();
ConfigurationElement addElement = modulesCollection.CreateElement("remove");
addElement["name"] = @"CartHeader";
//addElement["type"] = @"Contoso.ShoppingCart.Header";
//addElement["preCondition"] = @"managedHandler";
// Check if your CartHeader module exists
var exists = modulesCollection.Any(m => m.Attributes["name"].Value.Equals("CartHeader"));
// Handle accordingly
if (!exists)
{
// Create your module here
modulesCollection.Add(addElement);
serverManager.CommitChanges();
}
}
을 : 여기 https://www.iis.net/configreference/system.webserver/modules/add
내가 사용하려고 해요 C# 코드의 조각이다 : 여기
기사의 소스입니다 ?CreateElement ("remove")를 변경하고 요소를 추가하기 전에 검사를 추가했지만 요소를 계속 추가하기 때문에 요소를 <remove>
개로 간주하지 않습니다. 내가 놓친 게 있니?
당신은 모듈의 컬렉션을 반복 할 필요가 및 비교 추가하고 싶습니다. – Kami