在C#中部署后访问App.Config文件

时间:2015-10-15 11:37:55

标签: debugging service deployment

我使用下面的代码来更新app.config的一些值(我在app.config文件中有配置文件路径)。当部署它的获取错误时我认为它因为app.config文件改为exe 。如何将我的代码更改为调试时间以及部署时间

{{1}}

3 个答案:

答案 0 :(得分:1)

嗨,谢谢各方回复。我解决了自己的问题,这只是一个混乱。我是一个java人,我是.net在.net App.config文件中的新。编译并在Debug文件夹文件中创建.config,即使它调试它访问Debug文件夹中的.config文件。实际上,当你以编程方式更改App.config中的值时,它不会更改App.config文件。它改变调试文件中的.config,如调试文件夹中的[project name] .vshost.exe.config。

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");

使用上面的代码,您也可以在运行时更改App.config文件的调试时间值。但这些更改似乎不在App.config文件中。但你可以看到它所属的exe文件的变化。在我的情况下它是在src \ Vetserve.Credicare \ bin \ Debug \ Vetserve.Credicare.vshost.exe.config

答案 1 :(得分:0)

也许尝试将App.config的文件“复制到输出目录”属性设置为“始终复制”。

参考:AppConfig file not found in bin directory

答案 2 :(得分:0)

 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.
相关问题