0

错误:

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

1 回答 1

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;
}
于 2013-09-05T08:48:29.657 回答