读取app.config工作正常,但保存它会使应用程序崩溃



我已经构建了一个使用app.config文件的应用程序,并通过设置向导项目为其创建了Windows安装程序。当我运行已安装的应用程序时,它可以完美地读取配置,但是当我进行更改并保存配置时,应用程序就会崩溃。

这是我的代码:

Configuration applicationConfiguration = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Reading works just fine.
LogFilePath = applicationConfiguration.AppSettings.Settings["LogFilePath"].Value;
LogFilePath = "some string here";
applicationConfiguration.AppSettings.Settings["LogFilePath"].Value = LogFilePath;
// But saving it crashes the application. Only the .Save() line that making it crashed.
applicationConfiguration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

有什么想法这里怎么了?

由于文档为:https://msdn.microsoft.com/en-us/library/ms134265(V=VS.110).aspx可能是运行您地图的用户许可,但没有写入权限(文件和目录)

获取资源的配置对象, 您的代码必须在所有配置文件上具有读取权限 它从中继承了设置。要更新配置文件,请 代码还必须具有两个的写入权限 配置文件及其存在的目录。

最新更新