2016-07-08 2 views
0

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> 개로 간주하지 않습니다. 내가 놓친 게 있니?

+0

당신은 모듈의 컬렉션을 반복 할 필요가 및 비교 추가하고 싶습니다. – Kami

답변

1

당신은 가능성이 조회 및 특정 이름의 속성을 가진 요소가 Enumerable.Any() 방법을 통해 존재하는지 LINQ의 비트를 사용할 수 있습니다 것과 요소를

// Check if your CartHeader module exists 
var exists = modulesCollection.Any(m => m.Attributes["name"].Value.Equals("CartHeader")); 
// Handle accordingly 
if(!exist) 
{ 
    // Create your module here 
} 
+0

검색 결과에서 를 찾을 수 없습니다. Rod

+0

의 제안이 OP로 업데이트되면 검색이 제대로 작동합니다. – Rod

관련 문제