来自应用程序配置的奇怪值

Strange values coming from application config

我有使用 app.config 的 c# .net framework 4 客户端配置文件应用程序,内容如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="someKey" value="newValue"/>
  </appSettings>
</configuration>

当我安装应用程序时,我有文件 application.exe.config 具有相同的内容。

这就是问题所在。 当我以普通用户身份启动 application.exe 并从配置中加载值时

var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string value = configFile.AppSettings.Settings["someKey"].Value;

或者像这样

string value = ConfigurationManager.AppSettings["someKey"];

仅获取“oldValue”。但是当我以管理员身份启动应用程序时,我得到正确的“newValue”。

感谢回复。

我猜你是VirtualStore的受害者。

您以用户身份更改了 Program Files 文件夹下的配置文件,文件系统将更改写入用户 VirtualStore,a用户个人资料中的隐藏位置。

然后您以管理员身份启动了应用程序,该应用程序在 its VirtualStore 中没有该文件,因此使用存储在 Program Files[=24] 中的原始文件=].

修复的一种方法是为普通用户在 Program Files read/write 中创建配置文件 - 或者只需以具有写权限的管理员身份编辑配置文件。 (并确保再次为用户删除 VirtualStore 版本)。