简体   繁体   English

Visual Studio Settings.settings文件

[英]Visual Studio Settings.settings file

Is there a way of creating a new setting in the Settings.settings file during run time? 有没有一种在运行时在Settings.settings文件中创建新设置的方法?

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) 我知道如何从Settings.settings文件中获取值(值= Properties.Settings.Default.XXX)
  • I know how to update an existing value (Properties.Settings.Default.XXX = newValue; Properties.Settings.Default.Save()) 我知道如何更新现有值(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. 我想知道如何在运行时将“名称”,“类型”,“范围”和“值”添加到Settings.settings文件中。

Any suggestions would be appreciated! 任何建议,将不胜感激!

Thanks, Ivar 谢谢,伊瓦尔

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. 我相信Visual Studio在设计应用程序设置和值时会生成代码,因此在运行时这将不容易,而且最糟糕的是没有设计者。 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. 您会注意到,后面的代码具有您在设计器中创建的C#属性。 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. 您将不得不生成代码并将其动态地反馈给JIT编译器或类似的东西。 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: 您可能最终会用C#代码来管理它,如下所示:

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

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

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