簡體   English   中英

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

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

我有一個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";
    }

}

該TECHNIC似乎只為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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM