console application - app.config modify value c# -
the question addressed here app.config change value
the accepted answer is
string apppath = system.io.path.getdirectoryname(reflection.assembly.getexecutingassembly().location); string configfile = system.io.path.combine(apppath, "app.config"); execonfigurationfilemap configfilemap = new execonfigurationfilemap(); configfilemap.execonfigfilename = configfile; system.configuration.configuration config = configurationmanager.openmappedexeconfiguration(configfilemap, configurationuserlevel.none); config.appsettings.settings["yourthing"].value = "new value"; config.save();
but when tried implement same, found weird behavior. above answer throws nullreferenceexception
when setting value. config.appsettings.settings
count zero.
so set configfile path app.config path inside project
string configfile = @"d:\app\schedule\schedule\app.config";
this modifies both app.config inside project << appname >>.exe.config in bin/debug. don't think feasible solution application can deployed on path. hard coding configpath not work.
then again modified configfile given
string configfile = system.io.path.combine(apppath, "<appname>.exe.config");
the above code runs , modifies << appname >>.exe.config , not app.config inside project.
i not sure correct or missing thing.
i on vs 2012, c# 4.5 , console application. of there no other code in console , app.config
<configuration> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> <appsettings> <add key="yourthing" value="" /> </appsettings> </configuration>
this wrong understanding of app.config. expecting application modify source code incorrect.
when run program creates copy of app.config
bin/debug or bin/release folder.
your second solution fine. application working expected.
Comments
Post a Comment