简体   繁体   English

app.config修改值c#

[英]app.config modify value c#

The question is addressed here App.Config change value 这里提出的问题是App.Config的变化值

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 I tried to implement the same, I found a weird behavior. 但是当我试图实现同样的东西时,我发现了一种奇怪的行为。 The above answer throws NullReferenceException when setting the value. 上面的答案在设置值时抛出NullReferenceException The config.AppSettings.Settings count is zero. config.AppSettings.Settings计数为零。

So I set the configFile path to the app.config path inside project 所以我将configFile路径设置为项目中的app.config路径

string configFile = @"D:\App\Schedule\Schedule\App.config";

This modifies both app.config inside the project as well as << appname >>.exe.config which is in bin/debug. 这会修改项目中的app.config以及bin / debug中的<< appname >> .exe.config。 But I don't think this is a feasible solution as the application can be deployed on any path. 但我不认为这是一个可行的解决方案,因为应用程序可以部署在任何路径上。 So hard coding the configPath will not work. 所以对configPath进行硬编码是行不通的。

Then again I modified the configFile as given 然后我再次修改了给定的configFile

string configFile = System.IO.Path.Combine(appPath, "<appname>.exe.config");

The above code runs and modifies only the << appname >>.exe.config and not the app.config inside the project. 上面的代码只运行并修改了<< appname >> .exe.config而不是项目中的app.config。

I am really not sure which is correct or I am missing any thing. 我真的不确定哪个是正确的,或者我遗漏了任何东西。

I am on VS 2012, c# 4.5 and it is console application. 我在VS 2012,c#4.5,它是控制台应用程序。 As of now there is no other code in my console and the app.config is 截至目前,我的控制台中没有其他代码,app.config是

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
        <add key="YourThing" value="" />
    </appSettings>
</configuration>

This is just a wrong understanding of app.config. 这只是对app.config的错误理解。 You are expecting application to modify your source code which is incorrect. 您希望应用程序修改您的源代码是不正确的。

When you run your program it creates a copy of app.config to your bin/debug or bin/release folder. 运行程序时,它会创建app.config的副本到bin / debug或bin / release文件夹。

Your second solution is fine. 你的第二个解决方案很好。 Application is working as expected. 应用程序按预期工作。

Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
            config.AppSettings.Settings["PresentationFolder"].Value = path;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

Where: path - is "new value", "appSettings" - is Section in App.config 其中:path - 是“new value”,“appSettings” - 是App.config中的Section

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM