我有一个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,如here所示。你知道有没有办法做到这一点?
答案 0 :(得分:9)
您需要使用[]运算符并将整数转换为字符串,如下所示:
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");