2013-03-06 5 views

Antwort

2

Hier ist eine blog article, die Sie bekommen sollte, was Sie wollen. Aber um sicherzustellen, dass die Antwort verfügbar bleibt, werde ich den Code auch hier einfügen. Kurz gesagt, stellen Sie sicher, dass Sie auf die System.Configuration Assembly verweisen, und nutzen Sie dann die ConfigurationManager-Klasse, um genau die gewünschten Abschnitte zu erhalten.

using System; 
using System.Configuration; 

public class BlogSettings : ConfigurationSection 
{ 
    private static BlogSettings settings 
    = ConfigurationManager.GetSection("BlogSettings") as BlogSettings; 

    public static BlogSettings Settings 
    { 
    get 
    { 
     return settings; 
    } 
    } 

    [ConfigurationProperty("frontPagePostCount" 
    , DefaultValue = 20 
    , IsRequired = false)] 
    [IntegerValidator(MinValue = 1 
    , MaxValue = 100)] 
    public int FrontPagePostCount 
    { 
     get { return (int)this["frontPagePostCount"]; } 
     set { this["frontPagePostCount"] = value; } 
    } 


    [ConfigurationProperty("title" 
    , IsRequired=true)] 
    [StringValidator(InvalidCharacters = " [email protected]#$%^&*()[]{}/;’\"|\\" 
    , MinLength=1 
    , MaxLength=256)] 
    public string Title 
    { 
    get { return (string)this["title"]; } 
    set { this["title"] = value; } 
    } 
} 

Stellen Sie sicher, den Blog-Artikel zu lesen - es wird Ihnen den Hintergrund, so dass Sie es in Ihre Lösung passt.

Verwandte Themen