2013-04-08 35 views

回答

1

您可以使用它的「鬆散」類型化模式,基本上是看完了默認Web站點看起來像做:

using (ServerManager serverManager = new ServerManager()) 
{ 
    Configuration webConfig = serverManager.GetWebConfiguration("Default Web Site"); 
    ConfigurationSection section = webConfig.GetSection("system.webServer/defaultDocument"); 
    foreach (ConfigurationElement item in section.GetCollection("files")) 
    { 
     Console.WriteLine(item["value"]); 
    } 
} 

您還可以生成對集合強類型的包裝並且要經常使用,這使得它有很多清潔和防止錯誤的條目,這將使它看起來像:

using (ServerManager serverManager = new ServerManager()) 
{ 
    Configuration webConfig = serverManager.GetWebConfiguration("Default Web Site"); 
    DefaultDocumentSection section = (DefaultDocumentSection)webConfig.GetSection("system.webServer/defaultDocument", typeof(DefaultDocumentSection)); 
    foreach (FileElement item in section.Files) 
    { 
     Console.WriteLine(item.Value); 
    } 
} 

而對於工作,你甲腎上腺素編輯以下「強力包裝紙」:

public class DefaultDocumentSection : ConfigurationSection 
{ 
    private FilesCollection _files; 
    public FilesCollection Files 
    { 
     get 
     { 
      if (_files == null) 
      { 
       _files = (FilesCollection)base.GetCollection("files", typeof(FilesCollection)); 
      } 

      return _files; 
     } 
    } 

} 
public class FilesCollection : ConfigurationElementCollectionBase<FileElement> 
{ 
    protected override FileElement CreateNewElement(string elementTagName) 
    { 
     return new FileElement(); 
    } 
} 

public class FileElement : ConfigurationElement 
{ 
    public string Value { get { return (string)base["value"]; } } 
}