【问题标题】:how to get Properties.Settings.Default with a string?如何使用字符串获取 Properties.Settings.Default?
【发布时间】:2014-09-23 11:50:55
【问题描述】:

我有一个 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 所示。你知道有没有办法做到这一点?

【问题讨论】:

  • Properties.Settings.Default["name"+no.ToString()] = "value" ??
  • 好吧,很明显,我没有发现它会起作用! ://

标签: c# properties.settings


【解决方案1】:

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

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");

【讨论】:

  • 这是怎么回事?
猜你喜欢
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
  • 2019-03-10
  • 2013-03-30
  • 2014-07-08
  • 2017-05-27
相关资源
最近更新 更多