读取 App.config 工作正常但保存它会使应用程序崩溃
Reading App.config works fine but saving it crashes the application
我构建了一个包含 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可能运行您的地图的用户具有读取权限,但没有写入权限(对文件和目录)
To get the Configuration object for a resource,
your code must have read permissions on all the configuration files
from which it inherits settings. To update a configuration file, your
code must additionally have write permissions for both the
configuration file and the directory in which it exists.
我构建了一个包含 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可能运行您的地图的用户具有读取权限,但没有写入权限(对文件和目录)
To get the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists.