c# - Saving custom section to config file -


i have small issue, achieve following result in config file:

<customsection> <settings> <setting key="" value="" /> <setting key="" value=""/> ... </settings> </customsection> 

i've created following code, on first run cannot have created structure values pass!

here code initializes configuration code , builds default structure values:

configurationsectionmanager config = new configurationsectionmanager("customsection"); config.createdefaultconfigurationsection("customsection"); log("get value  = " + (config.getsection() configurationsectionmanager).settings[1].value); //instead of [1] key should set ... 

now code configuration section manager:

public class configurationsectionmanager: configurationsection {     private const string defaultsectionname = "default";      private string sectionname;     public string sectionname      {                  {             if (string.isnullorempty(sectionname))             {                 return defaultsectionname;             }             return sectionname;         }         set         {             sectionname = value;         }     }      public sancoconfigurationsectionmanager(string sectionname)     {         sectionname = sectionname;     }      public void createdefaultconfigurationsection(string sectionname)     {         configurationsectionmanager defaultsection = new configurationsectionmanager(sectionname);         settingsconfigurationcollection settingscollection = new settingsconfigurationcollection();         settingscollection[0] = new settingconfigurationelement() { key="element", value="element value" };         settingscollection[1] = new settingconfigurationelement() { key = "newelement", value = "nevalueelement" };         settingscollection[2] = new settingconfigurationelement() { key = "newelement2", value = "nevalueelement2" };         defaultsection.settings = settingscollection;         createconfigurationsection(sectionname, defaultsection);     }      public void createconfigurationsection(string sectionname, configurationsection section)     {         var config = configurationmanager.openexeconfiguration(null);         if (config.sections[sectionname] == null)         {             config.sections.add(sectionname, section);             section.sectioninformation.forcesave = true;             config.save(configurationsavemode.full);         }     }      public object getsection()     {         return configurationmanager.getsection(sectionname);     }      public override bool isreadonly()     {         return false;     }      public void save()     {         configuration config = configurationmanager.openexeconfiguration(null);         configurationsectionmanager instance = (configurationsectionmanager)config.sections[sectionname];         instance.settings = this.settings;         config.save(configurationsavemode.full);     }      [configurationproperty("settings", isrequired = false)]     public settingsconfigurationcollection settings      {          { return this["settings"] settingsconfigurationcollection; }         set { this["settings"] = value; }     }  } 

now code settings collection

public class settingsconfigurationcollection : configurationelementcollection {     public settingconfigurationelement this[int index]      {                   {              return baseget(index) settingconfigurationelement;          }          set          {              if (count > index && baseget(index) != null)              {                  baseremoveat(index);              }              baseadd(index, value);          }      }           protected override configurationelement createnewelement()      {          return new settingconfigurationelement();      }            protected override object getelementkey(configurationelement element)      {          return ((settingconfigurationelement)element).key;      } } 

and code setting elements

public class settingconfigurationelement : configurationelement {     [configurationproperty("key", isrequired = true)]     public string key      {          { return this["key"] string; }         set { this["key"] = value; }     }      [configurationproperty("value", isrequired = true)]     public string value      {          { return this["value"] string; }         set { this["value"] = value; }     }      public override bool isreadonly()     {         return false;     }  } 

when try of error:

unable load type 'myapp.utilities.configurationsectionmanager, myapp, version=1.0.0.0, culture=neutral, publickeytoken=cd112ea1ee9f48f2' because not public.    @ system.configuration.typeutil.getconstructorwithreflectionpermission(type type, type basetype, boolean throwonerror)    @ system.configuration.mgmtconfigurationrecord.addconfigurationsection(string group, string name, configurationsection configsection)    @ system.configuration.configurationsectioncollection.add(string name, configurationsection section) 

so cannot create settings etc ...

anyone has idea how make of work?

your configurationsectionmanager missing default constructor. add , execute.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -