c# - Settings value empty when main program not running -


i have put program settings values in separate project called common in solution. have main project (let call a), , other projects (b , c) , 1 common project. common added a, b , c's references. have tried accessing common's settings value using both:

  1. visual studio's settings.setting easy use tricky when comes reading other projects.
  2. a self-made ini file read , write to.

and realized both acting same way , think missing important point. setting values accessible when main project running. when update settings, saved , works fine.

but need access these values other projects while a not running. in case, project b triggered windows service. reads data database , executes batch file. needs access common's settings. , empty strings!

so if consider 2nd approach above (reading ini) common has value in conf static object (that use read , write settings) called connectionstring. value accessed in main program calling common.conf.connectionstring. when used b, value empty. cannot access settings values.

what might do, create function reads ini file every time need these values , parse them once again if common project not exist.

but there else can make work?

this code common's conf sealed class use read/write ini file.

namespace common {     public sealed class conf     {         public static string connectionstring { get; set; }         public static bool loggerenabled { get; set; }         public static string folderlocation { get; set; }         public static string delimeter { get; set; }          static readonly string settings = "conf.ini";         static readonly conf instance = new conf();         conf() { }         static conf()         {             string property = "";             string[] settings = file.readalllines(settings);             foreach (string s in settings)                 try                 {                     string[] split = s.split(new char[] { ':' }, 2);                     if (split.length != 2)                         continue;                     property = split[0].trim();                     string value = split[1].trim();                     propertyinfo propinfo = instance.gettype().getproperty(property);                     switch (propinfo.propertytype.name)                     {                         case "int32":                             propinfo.setvalue(null, convert.toint32(value), null);                             break;                         case "string":                             propinfo.setvalue(null, value, null);                             break;                         case "boolean":                             if (value == "1")                                 propinfo.setvalue(null, true, null);                             else                                 propinfo.setvalue(null, false, null);                             break;                     }                 }                 catch                 {                     throw new exception("invalid setting '" + property + "'");                 }         }          /// <summary>         /// save new configuration in conf.ini         /// </summary>         public static void saveconf()         {             stringbuilder sb = new stringbuilder();             propertyinfo[] properties = typeof(conf).getproperties();             type mytype = typeof(conf);             propertyinfo[] propertyinfos = mytype.getproperties(                    bindingflags.public | bindingflags.static | bindingflags.declaredonly);             foreach (propertyinfo p in propertyinfos)             {                 if (p.getvalue(mytype, null) == null)                     sb.appendline(string.format("{0}:{1}", p.name, string.empty));                 else                     sb.appendline(string.format("{0}:{1}", p.name, p.getvalue(mytype, null).tostring()));              }             string result = sb.tostring();             file.writealltext(settings, string.empty);             file.writealltext(settings, result);         }             } } 


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -