المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem wrting to custom config element



C# Programming
08-26-2009, 11:31 PM
I have created a custom configuration section in my config file. I can read from it with out any problem. How ever, When I try to write a new value to the element I get an error: "System.Configuration.ConfigurationErrorsException: The configuration is read only".
This is a desktop application so I wouldn't think it was because of insufient permissions. Any suggestions on how to solve this would be very much appreciated.

// This is what is in the config file













stringValue = "Veh8900"
/>


// This is the class I am using to access the custom config section.
namespace CSDBControl
{
class CustConfig: ConfigurationSection
{
private static ConfigurationProperty s_propString;

static CustConfig()
{
// Predefine properties here
s_propString = new ConfigurationProperty(
"stringValue",
typeof(string),
null,
ConfigurationPropertyOptions.IsRequired
);
}

// Gets/sets the StringValue setting.
[ConfigurationProperty("stringValue", IsRequired = true)]
public string StringValue
{
get { return (string)base[s_propString]; }
// **** This is where the read only error occurs
set { base[s_propString] = value; }
// Also tried:
// set { this[s_propString] = value; }
// set { this["stringValue"] = value; }
}
}
}

// I am accessing my configuration file like this:
string SelVehicles = "Vehicle1;
CustConfig custConfig = (CustConfig)ConfigurationManager.GetSection("custom");
// This works, I can read the value just fine
string test = custConfig.StringValue;
// This does not work. Not able to write to he config file.
custConfig.StringValue = SelVehicles;

Craig