简体   繁体   English

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

[英]how to get Properties.Settings.Default with a string?

I have a Winforms/C# application and I am migrating from xml configuration file to application settings. 我有一个Winforms / C#应用程序,我正在从xml配置文件迁移到应用程序设置。 I would like to know if it is possible to access dynamically application settings (Properties.Settings.Default). 我想知道是否可以动态访问应用程序设置(Properties.Settings.Default)。

I have 4 configurations possible for my application with the settings associated, which I logically named name1, name2, name3, name4, server1, server2 etc.. Instead of assigning them a value like 我有4种配置可用于我的应用程序与相关的设置,我逻辑上命名为name1,name2,name3,name4,server1,server2等。而不是为它们分配一个像这样的值

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

I would like to to something like this regarding the configuration they belong to : 关于它们所属的配置,我想做类似的事情:

class ApplicationSettings
{

    int no;

    ApplicationSettings(int no)
    {
        this.no = no;
    }

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

}

The technic seems to work only for SettingsProperties, as seen here . 该TECHNIC似乎只为SettingsProperties工作,因为看到这里 Do you know if there is a way to do this ? 你知道有没有办法做到这一点?

You need to use the [] operator and convert the integer to a string, like so: 您需要使用[]运算符并将整数转换为字符串,如下所示:

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();
    }

}

Usage 用法

ApplicationSettings.Save(1,"somesetting");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM