Đây là một blog article sẽ giúp bạn có được những gì bạn muốn. Nhưng để đảm bảo rằng câu trả lời vẫn có sẵn, tôi sẽ thả mã tại đây. Tóm lại, hãy đảm bảo bạn đang tham khảo assembly System.Configuration
và sau đó tận dụng lớp ConfigurationManager
để có được các phần rất cụ thể mà bạn muốn.
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; }
}
}
Đảm bảo bạn đọc bài viết trên blog - nó sẽ cung cấp cho bạn nền để bạn có thể phù hợp với giải pháp của mình.
Nguồn
2013-03-06 18:09:11