Visual Studio Settings.settings 文件

发布于 2024-09-06 22:21:37 字数 393 浏览 2 评论 0原文

有没有办法在运行时在 Settings.settings 文件中创建新设置?

例如,我想从一个类函数写入设置文件,并从另一个类函数读取该值。不,我不想传递这些价值观。

  • 我知道如何从 Settings.settings 文件中获取值 (value = Properties.Settings.Default.XXX)
  • 我知道如何更新现有值 (Properties.Settings.Default.XXX = newValue; Properties.Settings.Default.Save())

我想知道如何将“名称”、“类型”、“范围”和“值”添加到 Settings.settings 文件中在运行时。

任何建议将不胜感激!

谢谢, 伊瓦尔

Is there a way of creating a new setting in the Settings.settings file during run time?

For example, I want to write to the settings file from one class function, and read that value from a different class function. And no, I don't want to pass the values.

  • I know how to get values from the Settings.settings file
    (value = Properties.Settings.Default.XXX)
  • I know how to update an existing value
    (Properties.Settings.Default.XXX = newValue; Properties.Settings.Default.Save())

I want to know how I can add "Name", "Type", "Scope" and "Value" into the Settings.settings file during run time.

Any suggestions would be appreciated!

Thanks,
Ivar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-09-13 22:21:37

问题

我相信 Visual Studio 在设计应用程序设置和值时会生成代码,因此在运行时这并不容易,在最坏的情况下,如果没有设计人员,这是不可能的。然而,有时您可以在运行时调用设计功能。

您会注意到隐藏代码具有您在设计器中创建的 C# 属性。例如,我添加了一个设置:

Age [int]  30. 

代码隐藏已生成:(

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("30")]
public int Age {
   get {
       return ((int)(this["Age"]));
   }
   set {
       this["Age"] = value;
   }
}

代码生成是您拥有强类型设置的原因)

我不确定您是否可以在运行时实现同样的效果。您必须生成代码并将其动态反馈给 JIT 编译器或类似的东西。或者也许还有另一种我不知道的方式,以我对设置的有限理解。

建议/解决方法

我建议找出一种替代/更简单的方法,而不是跳过障碍。例如,设置一个可序列化的集合类型,以便它可以存储多个值。

例如,您可以在一种设置下存储多个年龄:

Ages  [System.Collections.ArrayList]  {add multiple values programatically}

您最终可能会使用 C# 代码来管理它,如下所示:

System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add("1");
list.Add("30");
Properties.Settings.Default.Ages = list;
Properties.Settings.Default.Save();

The Issues

I believe Visual Studio generates code when you design the application settings and values, therefore at runtime this would not be easy and at worst impossible without a designer. However you can sometimes call upon design features at runtime.

You'll notice the code-behind has the properties in C# that you create in your designer. For example, I added a setting for:

Age [int]  30. 

The code-behind has generated:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("30")]
public int Age {
   get {
       return ((int)(this["Age"]));
   }
   set {
       this["Age"] = value;
   }
}

(The code generation is why you have strongly-typed settings)

I'm unsure if you could effect this same thing at runtime. You would have to generate code and feed it back to the JIT compiler dynamically or something like that. Or maybe there's another way I don't know about in my limited understanding of settings.

Suggestion/Workaround

I'd suggest figuring out an alternate/easier way instead of jumping through hoops. For example, make one setting a collection type that is serializable so it can store multiple values.

Then you can, for example, store multiple ages under just one setting:

Ages  [System.Collections.ArrayList]  {add multiple values programatically}

You might end up with C# code to manage it like:

System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add("1");
list.Add("30");
Properties.Settings.Default.Ages = list;
Properties.Settings.Default.Save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文