繁体   English   中英

如何将新的属性添加到Properties.Settings?

[英]How do I add a new Property to Properties.Settings?

我想在运行时向Properties.Settings添加一个新的Property,但是我不知道它是如何工作的。 语言:c#我使用以下代码:

Properties.Settings.Default.Properties.Add(new System.Configuration.SettingsProperty(serviceName + "VersionsNr"));
Properties.Settings.Default.Save();

Properties.Settings.Default[serviceName + "VersionsNr"] = versionsNr;
Properties.Settings.Default.Save();

我得到了NullReferenceException。

设置属性名称是不够的。 您还应该至少定义属性属性和设置提供程序。 设置此信息的最简单方法是使用现有属性中的信息。 例如,如果您已经使用了从文件读取/存储属性值的设置,并且还打算使用配置文件,则解决方案如下:

        var existingProperty = Settings.Default.Properties["<existing_property_name>"];
        var property = new System.Configuration.SettingsProperty(
            "<new_property_name>",
            typeof(string),
            existingProperty.Provider,
            false,
            null,
            System.Configuration.SettingsSerializeAs.String,
            existingProperty.Attributes,
            false,
            false);
        Settings.Default.Properties.Add(property);

如果您需要新的东西,可以在此处找到示例代码。

暂无
暂无

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

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