有关写入app.config文件的问题
问题描述:
我从这篇文章中获得了一个示例.
这是App.config文件:
I have an example from the article.
here is the App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Setting1" value="Very" />
<add key="Setting2" value="Easy" />
</appSettings>
</configuration>
这是控制台程序write.cs:
here is the console program write.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace AppSettings
{
class Program
{
static void ShowConfig()
{
// For read access you do not need to call OpenExeConfiguraton
foreach(string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
}
static void Main(string[] args)
{
ShowConfig();
// Open App.Config of executable
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("ModificationDate",
DateTime.Now.ToLongTimeString() + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
ShowConfig();
}
}
}
我尝试过的事情:
我期望此ModificationDate将出现在App.config中,但事实证明是在write.exe.config文件中.为什么会这样?
What I have tried:
I am expecting this ModificationDate shall come up in App.config, but it turns out in write.exe.config file. why did this happen? the new variable can not be added into App.Config directly?
答
如注释中所述,Write.exe.config实际上就是您的app.config文件.它采用可执行文件的名称.
As mentioned in the comments, Write.exe.config actually IS your app.config file. It takes the name of the executable.