从config文件中读取数据
本帖最后由 yuanhaosh 于 2012-11-23 10:20:45 编辑 从config文件中读取数据在textbox中相对应的位置显示出来,
当config文件中有一两个一个一个取值还好,几十个一个一个取值不科学啊例如下边的
Sqlconnwords.Text = ConfigurationManager.ConnectionStrings["Sqlconnwords"].ToString();
PrimaryRecApIP.Text = ConfigurationManager.AppSettings["PrimaryRecApIP"];
PrimaryRecApPort.Text = ConfigurationManager.AppSettings["PrimaryRecApPort"];
BackupRecApIP.Text = ConfigurationManager.AppSettings["BackupRecApIP"];
SQWords.Text = ConfigurationManager.AppSettings["SQWords"];
BackupRecApPort.Text = ConfigurationManager.AppSettings["BackupRecApIP"];
LocalIp.Text = ConfigurationManager.AppSettings["BackupRecApIP"];
LocalPort.Text = ConfigurationManager.AppSettings["BackupRecApIP"];
MaxRetry.Text = ConfigurationManager.AppSettings["MaxRetry"];
DelayTime.Text = ConfigurationManager.AppSettings["DelayTime"];
BatchCount.Text = ConfigurationManager.AppSettings["BatchCount"];
resetTimePicker.Text = ConfigurationManager.AppSettings["resetTimePicker"];
disconTimePicker.Text = ConfigurationManager.AppSettings["disconTimePicker"];
NodeName.Text = ConfigurationManager.AppSettings["NodeName"];
LogType.Text = ConfigurationManager.AppSettings["LogType"];
RecordLength.Text = ConfigurationManager.AppSettings["RecordLength"];
MarketType.Text = ConfigurationManager.AppSettings["MarketType"];
LogPath.Text = ConfigurationManager.AppSettings["LogPath"];
DataPath.Text = ConfigurationManager.AppSettings["DataPath"];
将下边的如何改能赋给xx.text???
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (string key in ConfigurationManager.AppSettings.AllKeys)
dict.Add("key.Text", ConfigurationManager.AppSettings[key]);//该怎么将值传递
[最优解释]
config是XML的,直接加载到内存表里,再时行循环付值。这样只需要和物理文件交换一次。
[其他解释]
dict.Add(key+".Text", ConfigurationManager.AppSettings[key]);
[其他解释]
我想弄成这个这样的可是不知道用语法怎么表达(TextBox)key).Text 这个红色的key我在UI中同样命名textbox
[其他解释]
类似:
Control[] txt = null;
for (int i = 0; i < 10; i++)
{
txt = this.Controls.Find(string.Format("txt{0}", i), true);
if (null != txt)
(txt[0] as TextBox).Text = "";
}
[其他解释]
第二种:
Dictionary<string, string> cfg = new Dictionary<string, string>();
foreach (var item in ConfigurationManager.AppSettings.AllKeys)
{
cfg.Add(item, ConfigurationManager.AppSettings[item]);
}
List<TextBox> txtList = new List<TextBox>();
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
txtList.Add(c as TextBox);
}
}
foreach (var item in txtList)
{
foreach (KeyValuePair<string,string> kvp in cfg)
{
if (item.Name == kvp.Key)
item.Text = kvp.Value;
}
}
[其他解释]
我不理解这个东西 谢谢各位了
[其他解释]
Dictionary<string, string> cfg = new Dictionary<string, string>();
foreach (var item in ConfigurationManager.AppSettings.AllKeys)
{
cfg.Add(item, ConfigurationManager.AppSettings[item]);
}
[其他解释]
dict.Add(key+".Text", ConfigurationManager.AppSettings[key]);
这样是不是就对应好了,
dict["Sqlconnwords.Text"]=ConfigurationManager.ConnectionStrings["Sqlconnwords"].ToString()
[其他解释]
[其他解释]
将要赋值的控件的id和dict的key值一致然后再循环dict来赋值就可以了,不知道可行不!
[其他解释]
谷歌谷歌谷歌
[其他解释]
Dictionary<TextBox, string> cfg = new Dictionary<TextBox, string>();
cfg.Add(PrimaryRecApIP, "192.168.20.2");
foreach (System.Collections.Generic.KeyValuePair<TextBox,string> obj in cfg)
{
obj.Key.Text = "192.168.20.3";
obj.Key.Text = obj.Value;
}
/*
foreach (Control ct in this.groupBox1.Controls)
{
if (ct.GetType() == typeof(TextBox))
{
if (ct.Name.ToString() == "PrimaryRecApIP")
{
ct.Text = "192.168.20.2";
}
}
}*/
[其他解释]
null