views:

263

answers:

3

Hello,

could somebody tell me why I can not save dynamic data in Settings.Default.Context?

My code:

Settings.Default.Context.Add("myKey", "myValue");
Settings.Default.Save();

MessageBox.Show(Settings.Default.Context["myKey"].ToString());<-- This works

If I don't reload the appi everything works fine. But after reload application and calling only

MessageBox.Show(Settings.Default.Context["myKey"].ToString());<-- error on appi reload

then I get an error like Object reference not set to an instance of an object.. Why I can not save the context? What's the problem?

I'm using this way saving because of then I'm able to dynamically add new keys and values.

Best Regards!

A: 

uhm what is the type of Settings? Anyway I think you missed .Context ..try to write:

Settings.Default.Context["myKey"].ToString()

The problem is that the value is an User-Scope setting and persists only for the duration of the application session. I think you need application-scope settings which can only be changed at design time ( in the Project Properties -> Settings tab ) or by altering the .exe.config file in between application sessions ( http://msdn.microsoft.com/en-us/library/bb397744.aspx )

You have to do something like this:

    using System.Configuration;

namespace WindowsFormsApplication1
{
  class MySettings : ApplicationSettingsBase
  {
    [UserScopedSetting]
    public string SavedString
    {
      get { return ( string )this["SavedString"]; }
      set { this["SavedString"] = value; }
    }
  } 

  public partial class Form1 : Form
  {
    MySettings m_Settings;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load( object sender, EventArgs e )
    {
      m_Settings = new MySettings();

      Binding b = new Binding( "Text", m_Settings, "SavedString", true, DataSourceUpdateMode.OnPropertyChanged );
      this.DataBindings.Add( b );
    }

    private void Form1_FormClosing( object sender, FormClosingEventArgs e )
    {
      m_Settings.Save();
    }

    private void button1_Click( object sender, EventArgs e )
    {
      this.Text = "My Text";
    }
  }
}

This application creates a Form with no caption and a button in the center. Once clicked on the button the .Text ( so the caption ) changes and it's saved when closing the form. Rerun the application and you will have it with the new caption My Text :)

If you need the complete source code just tell me :)

Salv0
Sorry, I've changed the problem description.
Jooj
check the edit ;)
Salv0
A: 

Viewed in object browser:

public override SettingsContext Context { get; }

Read only?

Your Mom
It's not read only.
Jooj
A: 

I don't think you can add new settings this way, they're read only as they're resources of the program itself.

What you can do it make a setting that is a System.Collections.Specialized.StringCollection, then add items to it. These extra items will still be there after the app closes.

But you can't make a completely new setting and have it still be there.

Matt Warren