如何使用字符串获取Properties.Settings.Default?

我有一个Winforms / C#应用程序,我正在从xml配置文件迁移到应用程序设置。 我想知道是否可以动态访问应用程序设置(Properties.Settings.Default)。

我的应用程序可能有4个配置与相关的设置,我逻辑上命名为name1,name2,name3,name4,server1,server2等。而不是为它们分配一个像这样的值

Properties.Settings.Default.name1 = textbox.txt; 

关于它们所属的配置,我想这样的事情:

 class ApplicationSettings { int no; ApplicationSettings(int no) { this.no = no; } private void save() { Properties.Settings.Default.Properties["name"+no] = "value"; } } 

该技术似乎仅适用于SettingsProperties,如此处所示。 你知道有没有办法做到这一点?

您需要使用[]运算符并将整数转换为字符串,如下所示:

 internal static class ApplicationSettings { //added public static because I didn't see how you planned on invoking save public static void Save(int no, string value) { //sets the nameX Properties.Settings.Default["name"+no.ToString()] = value; //save the settings Properties.Settings.Default.Save(); } } 

用法

 ApplicationSettings.Save(1,"somesetting");