Ashan Ratnayake
Ashan Ratnayake

Reputation: 71

accessing App.Config file after deploy in C#

Im using below code to update app.config's some values( I have config file path in the app.config file).When deploy its getting errors I think its becouse app.config file change in to an exe. how to change my code work as debug time as well as deploy time

var appPath = ConfigurationManager.AppSettings["configPath"].ToString();
            string configFile = System.IO.Path.Combine(appPath, "App.config");
            var configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = configFile;
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

 config.AppSettings.Settings["InvoiceInterval"].Value = InvoiceIntervalVal.ToString();

Upvotes: 0

Views: 3077

Answers (3)

Ashan Ratnayake
Ashan Ratnayake

Reputation: 71

Hi thanks every body for instance reply. I fix my own problem it was just a confusion. I was a java guy and I new to .net in .net App.config file compile and create .config in Debug folder file even though debug it access that .config file in Debug folder . So actually when if you change the value in App.config in programatically it doesn't change the App.config file. it change the .config which is in debug file.its like [project name].vshost.exe.config in debug folder.

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["InvoiceInterval"].Value = InvoiceIntervalVal.ToString();
                config.AppSettings.Settings["directPaymentInterval"].Value = directPaymentIntervalVal.ToString();
                config.AppSettings.Settings["paymentStatusInterval"].Value = paymentStatusIntervalVal.ToString();
                config.Save();
                ConfigurationManager.RefreshSection("appSettings");

using above code you can able change App.config file's value in debug time also in run time. but those changes not seems in App.config file. but you can see changes in exe file which it is belongs to.In my case it was in src\Vetserve.Credicare\bin\Debug\Vetserve.Credicare.vshost.exe.config

Upvotes: 1

smaiti
smaiti

Reputation: 21

 After compilation App.Config will be available as   
 YourConsoleApplication.exe.Config inside application bin.
 You can do like:  
 var beforeInvoiceInterval = ConfigurationManager.AppSettings   
 ["InvoiceInterval"].ToString();
        ConfigurationManager.AppSettings["InvoiceInterval"] = "Your value";
 var afterInvoiceInterval = ConfigurationManager.AppSettings   
 ["InvoiceInterval"].ToString();

 afterInvoiceInterval will contain the value you assigned but it'll not
 modify YourConsoleApplication.exe.Config.

Upvotes: 1

Baltazar
Baltazar

Reputation: 1

Perhaps try and set your App.config's file 'Copy to Output Directory' property to 'Copy always'.

Reference: AppConfig file not found in bin directory

Upvotes: 0

Related Questions