Mylan
Mylan

Reputation: 115

C# dynamically add setting to settings file

I'm currently using the below code to add a setting to my settings file, but keep getting the following error:

The type 'providercollection' is defined in an assembly that is not referenced. You must add a reference to assembly" at this line: Properties.Settings.Default.Providers["LocalFileSettingsProvider"];

How can I fix this error? Thanks in advance.

  System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting");
  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);

Upvotes: 1

Views: 214

Answers (1)

Ian Vos
Ian Vos

Reputation: 69

You can do this:

  1. Go to solution explorer
  2. Right click on references
  3. Add reference
  4. search for System.Configuration
  5. Add System.Configuration

Upvotes: 1

Related Questions