如何构建由config File控制的动态字符串 [英] How to build dynamic string controlled by config File

查看:68
本文介绍了如何构建由config File控制的动态字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我想创建动态字符串,我可以从app.config文件控制



这个想法是功能与一些给定的信息:



Hello to all.

I want to create dynamic string that i can control from my app.config file

the idea is function with some given information:

data(hello,big,world);

public static void data(string a, string b, string c)
{
var appSettings = ConfigurationManager.AppSettings;

string ValueA =
string ValueB =
string ValueC =

MessageBox.Show(ValueA +" "+ ValueB +" "+ ValueC);
}







< pre lang =xml>

< add key =ValueAvalue =c/>

< add key =ValueBvalue =b/>

< add key =ValueCvalue =a/>< / pre>



我想得到的输出是:

世界大招你好



有人知道我该怎样才能实现它?



谢谢




<pre lang="xml">
<add key="ValueA" value="c"/>
<add key="ValueB" value="b"/>
<add key="ValueC" value="a"/></pre>

Whats i want to get as an output is:
world big hello

someone have an idea how can i achieve it ?

Thank you

推荐答案

使用字典(键值对)来保存值,而不是根据app.config文件读取它们;

Use a dictionary (key-value pairs) to keep the values than read them back according to app.config file;
public static void data(string a, string b, string c)
{
  Dictionary<string, string> oDic = new Dictionary<string, string> ( );

  oDic.Add ( "a", a );
  oDic.Add ( "b", b );
  oDic.Add ( "c", c );

  string ValueA = oDic[ ConfigurationManager.AppSettings[ "ValueA" ] ];
  string ValueB = oDic[ ConfigurationManager.AppSettings[ "ValueB" ] ];
  string ValueC = oDic[ ConfigurationManager.AppSettings[ "ValueC" ] ];
 
  MessageBox.Show( ValueA + " " + ValueB + " " + ValueC );
}


如果你需要有几个Type的键值对,你可以使用Type为'HashTable的Visual Studio Designer创建一个ApplictionSetting'字符串或任何其他类型。



1.进入Property Broweser的Project的Properties文件夹,然后双击Settings.Settings:这将打开Settings Designer 。



2.点击设置显示网格的其中一行,创建一个新设置。



3.点击'类型菜单选项打开类型浏览器:点击'浏览:打开mscorlib / Collections.Generic并选择'哈希表。



4.将设置范围设置为用户,并为新设置指定适当的名称;我们将在此示例中使用HashSetting(无引号)。



在代码中,使用哈希设置:
You can create an ApplictionSetting using the Visual Studio Designer of Type 'HashTable if you need to have several key-value pairs of Type 'string or any other Type.

1. go into your Project's Properties folder in the Property Broweser, and double-click Settings.Settings: this will bring up the Settings Designer.

2. create a new setting by clicking in one of the blank rows of the Settings display Grid.

3. click on the 'Type menu-option to open the Type Browser: click 'Browse: open mscorlib/Collections.Generic and selct 'HashTable.

4. Set the setting scope to 'User, and give the new Setting an appropriate name; we'll use "HashSetting" for this example (no quotes).

In code, to use the Hash Setting:
private Hashtable Hash;

private void btbAddToHash_Click(object sender, EventArgs e)
{
    Hash = new Hashtable
    {
        {"string1", "outcome1"},
        {"string2", "outcome2"}
    };

    // assign the current HashTable to the Setting's HashTable
    Properties.Settings.Default.HashSetting = Hash;

    // save the modified Setting(s)
    Properties.Settings.Default.Save();
}

// get a HashValue using its index
private string GetHashValue(string hashKey)
{
    return (string) Properties.Settings.Default.Hash1[hashKey];
}

// test in some method or EventHandler:
// assumes 'btnAddToHash was clicked before calling this

// test 1
string test1 = (string)Properties.Settings.Default.Hash1["string1"];

// test 2
string test2 = GetHashValue("string2");

Console.WriteLine("{0} : {1}", test1, test2);


这篇关于如何构建由config File控制的动态字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆