【问题标题】:C# retrieve app.config setting base on comboBox selectionC# 根据组合框选择检索 app.config 设置
【发布时间】:2018-03-17 22:46:09
【问题描述】:

我需要修改以下 C# 程序,从 config.app 中读取所有三个字母 abbreviationthe country name 并将其列在 winForm 组合框上,并从组合框中进行选择以相应地更新 lbCountry.TextlbCurrencyRate.Text。 我是 C# 新手,不确定如何使用以下详细信息更新 app.config 并根据组合框的选择检索它们。希望有人能帮忙。

国家缩写:美国
国名:美国
汇率:1.00

国家缩写:澳大利亚
国名:澳大利亚
汇率:0.80

国家缩写:GBR
国家名称:英国
汇率:0.76

国家缩写:JPN
国名:日本
汇率:113.00

以下是最初创建的部分代码,用于从 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

    <?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>

【问题讨论】:

  • 最好存储在单独的 xml 中。

标签: c# wpf winforms combobox app-config


【解决方案1】:

正如 Ramankingdom 所建议的,最好将数据存储在 .xml 文件中并使用 XDocument 类进行读取。 或者,您可以将数据存储在 .csv 文件中,并使用 CsvHelper 等第三方库进行读取。

【讨论】:

  • 是否可以在 app.config 而不是单独的 xml 中做到这一点?如果是这样,是否有任何示例代码,因为我不确定如何在 app.config 中执行并从组合框检索?
猜你喜欢
  • 2016-03-02
  • 2022-01-23
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多