6

我有一个 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,如此处所示。你知道是否有办法做到这一点?

4

1 回答 1

14

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

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");
于 2014-07-31T14:19:03.900 回答