ConfigurationManager: ConfigurationErrorsException after removing config attribute? [RESOLVED]
-
Exception message: "Unrecognized attribute 'valid'. Note that attribute names are case-sensitive. I used to have a field called "valid" in one of my configuration elements, but through the course of development it's now obsolete, so I'd like to remove it. I removed it from the ConfigurationElement subclass, and now when I try to read the config section (via config.GetSection), I get the above exception, which then makes my config section variable null, blah blah blah, no saved config = program effectively unusable from session to session. Google fails me. MSDN fails me. Internet fails me. I'm new to the ConfigurationManager, having only ever done equivalent functionality through .ini files before, so I'm sure there's probably just some "cleanup" command I can do that I'm just not aware of.
modified on Friday, October 23, 2009 2:41 PM
-
Exception message: "Unrecognized attribute 'valid'. Note that attribute names are case-sensitive. I used to have a field called "valid" in one of my configuration elements, but through the course of development it's now obsolete, so I'd like to remove it. I removed it from the ConfigurationElement subclass, and now when I try to read the config section (via config.GetSection), I get the above exception, which then makes my config section variable null, blah blah blah, no saved config = program effectively unusable from session to session. Google fails me. MSDN fails me. Internet fails me. I'm new to the ConfigurationManager, having only ever done equivalent functionality through .ini files before, so I'm sure there's probably just some "cleanup" command I can do that I'm just not aware of.
modified on Friday, October 23, 2009 2:41 PM
-
I believe you are writing a class to read configuration values from a config class. You have removed the 'valid' element from the spec but not from the config file. I think, removing the 'valid' element from the config file should solve your problem.
-
Yes, that's more or less the problem, but since it's using the ConfigurationManager, the actual config file operations are completely hidden (as far as I know), or at least I don't know where to find it.
-
What kind of app r u developing? Windows or Web ? Whatever it is you should have full control over your config files.
It's a Windows app using the built-in ConfigurationManager .NET capabilities. I based it on this MSDN code sample[^]. Maybe it'll be easier to just show some code: Configuration classes:
public class SerialConfig : ConfigurationElement
{
/* *********************************** */
/* This is the field I want to remove! */
/* *********************************** */
// Fields
[ConfigurationProperty("valid",
DefaultValue = false,
IsRequired = false)]
//private int baud;
public bool Valid
{
get
{
return (bool)this["valid"];
}
set
{
this["valid"] = (bool)value;
}
}\[ConfigurationProperty("baud", DefaultValue = 115200, IsRequired = true)\] //private int baud; public int Baud { get { return (int)this\["baud"\]; } set { if((value == 115200) || (value == 9600)) { this\["baud"\] = value; } else { } } } \[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)\] //private String name; public String Name { get { return (string)this\["name"\]; } set { if (value.Contains("COM")) { this\["name"\] = value; } else { this\["name"\] = "INVALID"; } } } // Constructors public SerialConfig(String name, int baud) { this\["baud"\] = baud; this\["name"\] = name; } public SerialConfig(string name) { new SerialConfig(name, 115200); } public SerialConfig() { new SerialConfig("INVALID", 115200); } } // Define a c
-
It's a Windows app using the built-in ConfigurationManager .NET capabilities. I based it on this MSDN code sample[^]. Maybe it'll be easier to just show some code: Configuration classes:
public class SerialConfig : ConfigurationElement
{
/* *********************************** */
/* This is the field I want to remove! */
/* *********************************** */
// Fields
[ConfigurationProperty("valid",
DefaultValue = false,
IsRequired = false)]
//private int baud;
public bool Valid
{
get
{
return (bool)this["valid"];
}
set
{
this["valid"] = (bool)value;
}
}\[ConfigurationProperty("baud", DefaultValue = 115200, IsRequired = true)\] //private int baud; public int Baud { get { return (int)this\["baud"\]; } set { if((value == 115200) || (value == 9600)) { this\["baud"\] = value; } else { } } } \[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)\] //private String name; public String Name { get { return (string)this\["name"\]; } set { if (value.Contains("COM")) { this\["name"\] = value; } else { this\["name"\] = "INVALID"; } } } // Constructors public SerialConfig(String name, int baud) { this\["baud"\] = baud; this\["name"\] = name; } public SerialConfig(string name) { new SerialConfig(name, 115200); } public SerialConfig() { new SerialConfig("INVALID", 115200); } } // Define a c
Your solution must have a default configuration file (app.config) that is used as a template to create local copies during deployment. For example, if you app name is myapp.exe, then your configuration file name during deployment is myapp.exe.config. Changing the app.config should solve your problem.
-
It's a Windows app using the built-in ConfigurationManager .NET capabilities. I based it on this MSDN code sample[^]. Maybe it'll be easier to just show some code: Configuration classes:
public class SerialConfig : ConfigurationElement
{
/* *********************************** */
/* This is the field I want to remove! */
/* *********************************** */
// Fields
[ConfigurationProperty("valid",
DefaultValue = false,
IsRequired = false)]
//private int baud;
public bool Valid
{
get
{
return (bool)this["valid"];
}
set
{
this["valid"] = (bool)value;
}
}\[ConfigurationProperty("baud", DefaultValue = 115200, IsRequired = true)\] //private int baud; public int Baud { get { return (int)this\["baud"\]; } set { if((value == 115200) || (value == 9600)) { this\["baud"\] = value; } else { } } } \[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)\] //private String name; public String Name { get { return (string)this\["name"\]; } set { if (value.Contains("COM")) { this\["name"\] = value; } else { this\["name"\] = "INVALID"; } } } // Constructors public SerialConfig(String name, int baud) { this\["baud"\] = baud; this\["name"\] = name; } public SerialConfig(string name) { new SerialConfig(name, 115200); } public SerialConfig() { new SerialConfig("INVALID", 115200); } } // Define a c
-
In the InitializeConfig() method, debug and check the value of userConfig.FilePath property. I think that should tell you the configuration file path.