修改的app.config值c# [英] app.config modify value c#
问题描述
现在的问题是在这里解决 C#App.Config中变化值
接受的答案是
字符串APPPATH = System.IO.Path。 GetDirectoryName(Reflection.Assembly.GetExecutingAssembly()位置。);
串CONFIGFILE = System.IO.Path.Combine(APPPATH的App.config);
ExeConfigurationFileMap configFileMap =新ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = CONFIGFILE;
System.Configuration.Configuration配置= ConfigurationManager.OpenMappedExeConfiguration(configFileMap,ConfigurationUserLevel.None);
config.AppSettings.Settings [YourThing]值=新价值。
config.Save();
但是,当我试图执行相同的,我发现了一个怪异的行为。设定值时,以上回答抛出的NullReferenceException
。在 config.AppSettings.Settings
计数为零。
所以我设置CONFIGFILE路径的app.config路径内部项目
字符串CONFIGFILE = @D:\App\Schedule\Schedule\App.config
这会修改项目中都的app.config以及为<< APPNAME >>。exe.config这是斌/调试。但我不认为这是一个可行的解决方案的应用程序可以部署任何路径上。所以硬编码用configPath将无法工作。
然后我又修改了CONFIGFILE给出
字符串CONFIGFILE = System.IO.Path.Combine(APPPATH,<应用程序的名字> .exe.config);
上面的代码运行,并修改只在<< APPNAME >>。exe.config,而不是项目中的app.config中。
我真的不知道这是正确的还是我错过任何东西。
我在VS 2012,C#4.5,它是控制台应用程序。截至目前已经有在我的控制台没有其他代码在app.config是
<结构>
<&启动GT;
< supportedRuntime版本=V4.0SKU =.net框架,版本= V4.5/>
< /启动>
<&的appSettings GT;
<添加键=YourThingVALUE =/>
< /的appSettings>
< /结构>
这是app.config中只是一个错误的认识。
你期待的应用程序来修改你的源代码,这是不正确。
当你运行程序会创建应用程序的副本。配置
来你的bin / Debug或Bin /释放文件夹中。
您第二个解决方案是好的。应用程序按预期工作。
The question is addressed here c# 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 I tried to implement the same, I found a weird behavior. The above answer throws NullReferenceException
when setting the value. The config.AppSettings.Settings
count is zero.
So I set the configFile path to the app.config path inside project
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. 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.
Then again I modified the configFile as given
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.
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. As of now there is no other code in my console and the app.config is
<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. 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.
Your second solution is fine. Application is working as expected.
这篇关于修改的app.config值c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!