c#无法写入app.config文件
我有以下代码向app.config文件appSettings部分添加一个新的键值对c#无法写入app.config文件,c#,.net,visual-studio,c#-4.0,app-config,C#,.net,Visual Studio,C# 4.0,App Config,我有以下代码向app.config文件appSettings部分添加一个新的键值对 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings.Add("Hello", "World"); string name = config.AppSetti
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("Hello", "World");
string name = config.AppSettings.Settings["Hello"].Value;
config.Save(ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\xxx\xml.txt");
file.WriteLine(name);
file.Close();
当我运行应用程序并打开app.config文件时,没有任何更改。但是,字符串
World
写在xml.txt上,这意味着这个键值对确实存在。您实际上是在查看app.config
文件,还是从app.config
生成的文件,例如HelloWorld.exe.config
?app.config文件。我还尝试在c驱动器中搜索“HelloWorld”,但什么也没有显示@Jonskeet可能是你查错了文件的副本app.config
实际上就像一个“源”文件——在构建时,它被复制到Foo.exe.config
旁边的Foo.exe
。当一个配置被保存时,它被保存到Foo.exe.config
——因为这是用户机器上所有可用的配置。你说得对@ShakirAhamed