gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:26:19 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c# - 如何使用字符串获取 Properties.Settings.Default?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25060519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com