在类型对象上找不到 C# 属性

本文关键字:属性 找不到 类型 对象 | 更新日期: 2023-09-27 18:30:18

>错误:

System.Configuration.SettingsPropertyNotFoundException 未处理 找不到设置属性自定义设置

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
namespace Addsettings
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void radButton1_Click(object sender, EventArgs e)
        {
            radTextBox1.Text = (string)Properties.Settings.Default["CustomSetting"];
        }
        private void radButton2_Click(object sender, EventArgs e)
        {
            System.Configuration.SettingsProperty property = new 
            System.Configuration.SettingsProperty("CustomSetting");
            property.SerializeAs = SettingsSerializeAs.Xml;
            property.DefaultValue = "Default";
            property.IsReadOnly = false;
            property.PropertyType = typeof(string);
            property.Provider = 
            Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
        property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
            Properties.Settings.Default.Properties.Add(property);
            // Load settings now.
            Properties.Settings.Default.Reload();
            // Update the user itnerface.
            Properties.Settings.Default["CustomSetting"] = radTextBox1.Text;
            Properties.Settings.Default.Save();
        }
    }
}

在类型对象上找不到 C# 属性

编辑 完全误解的要求。如果尝试在运行时创建此设置,请稍后再次阅读,下面是代码更改:

//ensures no runtime errors if you try and view the setting before its created
private bool _customSettingExists = false;
public Form1()
{
    InitializeComponent();
}
private void radButton1_Click(object sender, EventArgs e)
{
    //You're saving your CustomSetting to properties, so you should read it from Default.Properties
    if (_customSettingExists)
        radTextBox1.Text = Properties.Settings.Default.Properties["CustomSetting"].ToString();
}
private void radButton2_Click(object sender, EventArgs e)
{
    System.Configuration.SettingsProperty property = new
    System.Configuration.SettingsProperty("CustomSetting");
    property.SerializeAs = SettingsSerializeAs.Xml;
    property.DefaultValue = "Default";
    property.IsReadOnly = false;
    property.PropertyType = typeof(string);
    property.Provider =
    Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
    property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
    Properties.Settings.Default.Properties.Add(property);
    // Load settings now.
    Properties.Settings.Default.Reload();
    // Update the user itnerface.
    Properties.Settings.Default.Properties["CustomSetting"] = radTextBox1.Text;
    Properties.Settings.Default.Save();
    //now that you know a custom setting exists
    _customSettingExists = true;
}