简体   繁体   English

如何在运行时在app.config中创建新用户设置

[英]How to Create New User Setting in app.config at Run Time

I have an Editable ComboBox. 我有一个可编辑的组合框。 The user enters text and presses a Save button. 用户输入文本,然后按保存按钮。 Their text is turned into a string. 他们的文字变成了字符串。

I need it at Run Time to Create a new User Setting to the app.config with the name of their string. 我需要在运行时使用其字符串名称为app.config创建新的用户设置。 (I think this part works now). (我认为这部分现在可以使用)。

Then another ComboBox's Selected Item is saved to the Setting. 然后,另一个ComboBox的Selected Item保存到设置中。 (Object reference not set error). (对象引用未设置错误)。

This is to create a custom preset that will save each control state, checkboxes, textboxes, etc. in the program. 这是为了创建一个自定义预设,该预设将保存程序中的每个控件状态,复选框,文本框等。

// Control State to be Saved to Setting
Object comboBox2Item = ComboBox2.SelectedItem;

// User Custom Text
string customText = ComboBox1.Text;

// Create New User Setting
var propertyCustom = new SettingsProperty(customText);
propertyCustom.Name = customText;
propertyCustom.PropertyType = typeof(string); 
Settings.Default.Properties.Add(propertyCustom);

// Add a Control State (string) to the Setting
Settings.Default[customText] = (string)comboBox2Item;

At this part I get an error. 在这一部分,我得到一个错误。

Settings.Default[customText] = (string)comboBox2Item;

Exception:Thrown: "Object reference not set to an instance of an object." 抛出:“对象引用未设置为对象的实例。”

I have tried setting ComboBox1.Text to an Object instead of string, with same error. 我尝试将ComboBox1.Text设置为对象而不是字符串,出现相同错误。 The text and string is also not null. 文本和字符串也不为空。

Object customText = ComboBox1.Text;

Here's a visual of what I'm trying to do 这是我正在尝试做的视觉效果 自定义用户设置

Original Answer: 原始答案:

I haven't tried adding a new setting to the file but i have had to update it. 我没有尝试向文件添加新设置,但是我不得不对其进行更新。 Here is some code that I use to save and retrieve the saved changes to the file. 这是一些代码,用于保存和检索文件的已保存更改。 I know it doesn't directly answer the question but should point you in the right direction as to what classes to look at and use. 我知道它并不能直接回答问题,但是应该为您指出要使用和使用的类的正确方向。

I'll try to update to directly answer this question once I get some breathing time. 喘口气之后,我将尝试更新以直接回答此问题。

public static void UpdateConfig(string setting, string value, bool isUserSetting = false)
    {
        var assemblyPath = AppDomain.CurrentDomain.BaseDirectory;
        var assemblyName = "AssemblyName";

        //need to modify the configuration file, launch the server with those settings.
        var config =
            ConfigurationManager.OpenExeConfiguration(string.Format("{0}\\{1}.exe", assemblyPath, "AssemblyName"));

        //config.AppSettings.Settings["Setting"].Value = "false";
        var getSection = config.GetSection("applicationSettings");
        Console.WriteLine(getSection);

        var settingsGroup = isUserSetting
            ? config.SectionGroups["userSettings"]
            : config.SectionGroups["applicationSettings"];
        var settings =
            settingsGroup.Sections[string.Format("{0}.Properties.Settings", assemblyName)] as ClientSettingsSection;
        var settingsElement = settings.Settings.Get(setting);

        settings.Settings.Remove(settingsElement);
        settingsElement.Value.ValueXml.InnerText = value;
        settings.Settings.Add(settingsElement);

        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

Edited Answer: 编辑答案:

I did a quick google search and found an accepted answer on the MSDN forum. 我做了一个快速的谷歌搜索,并在MSDN论坛上找到了可接受的答案。 MSDN question . MSDN问题 You have to call save on the properties class in order for the add to take affect. 您必须在属性类上调用save才能使添加生效。 Think of a database transaction, until you call commit, it doesn't take effect. 考虑一下数据库事务,除非调用commit,否则它不会生效。

So what appears to be missing in your code is: Properties.Settings.Default.Save(); 因此,您的代码中似乎缺少的是: Properties.Settings.Default.Save(); which should be the very next line after your Settings.Default.Properties.Add(propertyCustom); 这应该是Settings.Default.Properties.Add(propertyCustom);之后的下一行Settings.Default.Properties.Add(propertyCustom);

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

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