简体   繁体   English

C#基于comboBox选择检索app.config设置

[英]C# retrieve app.config setting base on comboBox selection

I will need to modify the following C# program to read all of the three letters abbreviationthe country name from config.app and list it on winForm comboBox, and making selection from comboBox to update the lbCountry.Text and lbCurrencyRate.Text accordingly. 我将需要修改以下C#程序,以从config.app中读取所有三个字母的缩写国家名称,并将其在winForm comboBox上列出,并从comboBox中进行选择以相应地更新lbCountry.TextlbCurrencyRate.Text I am new on C#, not sure how to update app.config with the following details and retrieve them base on the selection of comboBox. 我是C#的新手,不确定如何使用以下详细信息更新app.config并根据comboBox的选择检索它们。 Hope someone can help. 希望有人能帮忙。

Country Abbr: USA 国家Abbr:美国
Country Name: United States of America 国家/地区名称:美国
Currency Rate: 1.00 货币汇率:1.00

Country Abbr: AUS 国家(地区):AUS
Country Name: Australia 国家名称:澳大利亚
Currency Rate: 0.80 货币汇率:0.80

Country Abbr: GBR 国家(地区):GBR
Country Name: United Kingdom 国家名称:英国
Currency Rate: 0.76 货币汇率:0.76

Country Abbr: JPN 国家Abbr:JPN
Country Name: Japan 国家名称:日本
Currency Rate: 113.00 货币汇率:113.00

Following are the partial code that was originally created to retrieve one country details from app.config. 以下是最初为从app.config中检索一个国家/地区详细信息而创建的部分代码。

    private void Main_Load(object sender, EventArgs e)
    {
        string countryName = ConfigurationManager.AppSettings.Get("COUNTRY_NAME");
        string currencyRate = ConfigurationManager.AppSettings.Get("CURRENCY_RATE");

        lbCountry.Text = string.Format("Country Name: {0}", countryName.ToString());
        lbCurrencyRate.Text = string.Format("Currency Rate: {0}", currencyRate.ToString());

        ......
    }

App.config App.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="COUNTRY_NAME" value="United States of America" />
        <add key="CURRENCY_RATE" value="1.00" />
      </appSettings>
      ......
    </configuration>

As suggested by Ramankingdom it's better to store the data in an .xml file and read using the XDocument class. 如Ramankingdom所建议,最好将数据存储在.xml文件中,并使用XDocument类进行读取。 Or you can store data in an .csv file and read with third party library like CsvHelper. 或者,您可以将数据存储在.csv文件中,并使用CsvHelper之类的第三方库进行读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM