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

时间:2013-09-05 08:25:48

标签: c# winforms visual-studio-2012

错误:

  

System.Configuration.SettingsPropertyNotFoundException 未处理       找不到设置属性 CustomSetting

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

//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;
}