2012-10-13 56 views
10

Tôi là người mới bắt đầu với phần cấu hình trong C#
Tôi muốn tạo phần tùy chỉnh trong tệp cấu hình. Những gì tôi đã cố gắng sau khi googling là như sau
tập tin Config:
Phần Cấu hình tùy chỉnh trong App.config C#

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="MyCustomSections"> 
     <section name="CustomSection" type="CustomSectionTest.CustomSection,CustomSection"/> 
    </sectionGroup> 
    </configSections> 

    <MyCustomSections> 
    <CustomSection key="Default"/> 
    </MyCustomSections> 
</configuration> 


CustomSection.cs

namespace CustomSectionTest 
{ 
    public class CustomSection : ConfigurationSection 
    { 
     [ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] 
     public String Key 
     { 
      get { return this["key"].ToString(); } 
      set { this["key"] = value; } 
     } 
    } 
} 


Khi tôi sử dụng mã này để lấy Mục I gặp lỗi khi nói lỗi cấu hình.

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection"); 


tôi đang thiếu gì?
Cảm ơn.

Sửa
Những gì tôi cần cuối cùng là

<CustomConfigSettings> 
    <Setting id="1"> 
     <add key="Name" value="N"/> 
     <add key="Type" value="D"/> 
    </Setting> 
    <Setting id="2"> 
     <add key="Name" value="O"/> 
     <add key="Type" value="E"/> 
    </Setting> 
    <Setting id="3"> 
     <add key="Name" value="P"/> 
     <add key="Type" value="F"/> 
    </Setting> 
</CustomConfigSettings> 
+1

Vui lòng xem lại các câu hỏi sau: http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection http://stackoverflow.com/questions/7983127/custom- cấu hình http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location – bytebuster

Trả lời

30

App.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="customAppSettingsGroup"> 
     <section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </sectionGroup> 
    </configSections> 
    <customAppSettingsGroup> 
    <customAppSettings> 
     <add key="KeyOne" value="ValueOne"/> 
     <add key="KeyTwo" value="ValueTwo"/> 
    </customAppSettings> 
    </customAppSettingsGroup> 
</configuration> 

Cách sử dụng:

NameValueCollection settings = 
    ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings") 
    as System.Collections.Specialized.NameValueCollection; 

if (settings != null) 
{ 
foreach (string key in settings.AllKeys) 
{ 
    Response.Write(key + ": " + settings[key] + "<br />"); 
} 
} 
+13

Đối với bất kỳ ai muốn làm việc này nhanh chóng. 3 điều: 1. BẠN PHẢI THÊM một tham chiếu đến System.Configuration trong tài liệu tham khảo của bạn, 2. using System.Configuration; 3. sử dụng System.Collections.Specialized; –

+2

Bạn cũng có thể bỏ qua nhóm (customAppSettingsGroup) nếu bạn muốn. – Jess

1

Thử sử dụng:

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("MyCustomSections/CustomSection"); 

Bạn cần cả hai tên của nhóm phần và các mục tùy chỉnh.

+0

Cảm ơn phản hồi của bạn nhưng điều này không hiệu quả đối với tôi bằng cách nào đó. hãy xem phần đã chỉnh sửa của câu hỏi. –

-2

Highlight ConfigurationSection bấm F1, Bạn sẽ thấy rằng việc thực hiện trên trang web MSDN ghi đè một tính chất gọi là "Properties "trả về một" ConfigurationPropertyCollection ", vì các thuộc tính của bạn có một thuộc tính phù hợp với kiểu đó, bạn sẽ có thể điền vào coll này ection với tài sản của bạn nếu không quấn chúng trong cùng một cách MS guys có.

Các vấn đề liên quan