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

مشاهدة النسخة كاملة : app.config



C# Programming
08-24-2009, 08:24 AM
I want to have a custom configuration . I implement Test class as follow:

public sealed class Test : ConfigurationSection
{
private static ConfigurationPropertyCollection _Properties;
private static readonly ConfigurationProperty _ID =
new ConfigurationProperty("ID", typeof(string),"1");
private static readonly ConfigurationProperty _status =
new ConfigurationProperty("status", typeof(string), "1");
private static readonly ConfigurationProperty _color =
new ConfigurationProperty("color", typeof(string), "1");
public Test()
{
// Property initialization
_Properties =
new ConfigurationPropertyCollection();

_Properties.Add(_ID);
_Properties.Add(_status);
_Properties.Add(_color);


}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _Properties;
}
}

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
public string ID
{
get
{
return (string)this["ID"];
}
set
{
this["ID"] = value;
}
}

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
public string status
{
get
{
return (string)this["status"];
}
set
{
this["status"] = value;
}
}

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
public string color
{
get
{
return (string)this["color"];
}
set
{
this["color"] = value;
}
}

}
To retrieve my custom setting data the using code is as follow. Config can recognize "mySection" but the myConfig is always null. How can I solve this problem.

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None) as Configuration;
Test myConfig = new Test();
myConfig = config.GetSection("mySection") as Test;





type="Mina.Test,Configuration, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null"
allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="false"
/>

< mySection >
;
;
;



tahereh sadeghi