如何在ConfigurationManager中查找AppSettings的索引? [英] How Can I Find Index Of AppSettings in ConfigurationManager?

查看:58
本文介绍了如何在ConfigurationManager中查找AppSettings的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下函数获取应用程序设置的值时

它要我写它的名称和索引。

when I use below function to get value of app settings
it want me to write name and index of it.

ConfigurationManager.AppSettings.Get(string name or int index);



我不想在我的代码中使用字符串(名称)。我想使用索引,但我不知道什么是一个设置的索引?如果我添加一些其他设置,我该如何找到它以及索引将如何变化?



to(parveen以及我接受他的回答):

坦克你这很好,但我不知道为什么他们在无法获得设置索引时写这个函数?因此,当使用此功能时?

我想在消息框中保存我的消息,在应用程序设置中加密,当有人反编译我的代码时,这可能有助于我的应用程序保护(字符串不在我的代码中) 。除了我选择的这种方法还有其他方法吗?


I don't want to use string (name ) in my code. I want to use index but I don't know that what is index of one setting ? how can I find it and how index will change if I add some other setting?

to( parveen and who I accept his answer):
tank you It is good , but I don't know that why they writ this function when there is no way to get index of setting ? thus when this function will be used?
I want to save my message in message box with encryption in app setting and when someone decompile my code this may help my app protection (string is not in my code) . is there other way to do this than this approach that I selected?

推荐答案

你可以试试这样的事情



You can try somthing like this

var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
              var settings = configFile.AppSettings.Settings;

              var keylist = settings.AllKeys;
              // here in keylist variable you will get list of keys from config
              //   so you can access them by passing their array position
              //  i.e. To get the value : var temp = keylist[0];
              //  i.e. To set the value : keylist[0] = "some value";

              configFile.Save(ConfigurationSaveMode.Modified);
              ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);







希望这个将解决您的问题。




Hope this will solve your problem.


1。通过使用索引来访问设置没有指示(并且不可行),因为在 MSDN [ ^ ]:此类型的集合不保留元素的顺序,枚举集合时不保证特定顺序。



2.所以解决方案是使用字符串键而不是数字索引来访问!
1. It is not indicated (and not fiable) to access the settings by using index, because like is said in MSDN[^]: "Collections of this type do not preserve the ordering of element, and no particular ordering is guaranteed when enumerating the collection."

2. So the solution is to access by using the string key and not by numeric index!


这篇关于如何在ConfigurationManager中查找AppSettings的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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