尝试 Configuration.Save() 在 Program Files 中安装时访问被拒绝
Access denied when attempt Configuration.Save() for installation in Program Files
当 WPF 程序安装在“Program Files”下时,尝试使用 Configuration.Save()
失败。如果安装在别处,Save()
会按预期工作。程序本身没有被授权编辑自己的 .config 文件吗?
我有两个例外:
System.Configuration: An error occurred loading a configuration file: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied. (C:\Program Files\Advanced Applications\ConfigurationTest\WPF.exe.Config)
mscorlib: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied.
我尝试用 ConfigurationUserLevel.PerUserRoaming)
而不是 ConfigurationUserLevel.None
打开,但它仍然失败,报告了不同的异常:
System.Configuration: An error occurred executing the configuration section handler for appSettings.
System.Configuration: ConfigurationSection properties cannot be edited when locked.
[注意:搜索发现了一篇看似相关但未提供此问题解决方案的文章:
Access to path denied for App.Config on Windows 8. How can I update App.Config?
前两个方法从 MainVM 调用并调用底层基本方法,这些方法期望在传入的对象上找到相关 属性,然后同步 AppSettings 中的适当值。
private void SettingsLoad()
{
SettingsLoad(this);
}
private void SettingsSave()
{
try
{
SettingsSave(this);
}
catch (Exception ex)
{
currentMessageAction = MessageAction.Acknowledge;
window.MessagePanel.Show("EXCEPTION!", Utility.ParseException(ex));
}
}
....
public static void SettingsLoad(object main)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
// Attempt to get a reference to the property and, if found, set its value
var prop = main.GetType().GetProperty(setting.Key);
if (prop != null)
{
prop.SetValue(main, setting.Value);
}
}
}
public static void SettingsSave(object main)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
// Attempt to get a reference to the property and, if found, update its value
var prop = main.GetType().GetProperty(setting.Key);
if (prop != null)
{
setting.Value = prop.GetValue(main).ToString();
}
}
// This line produces an exception
config.Save(ConfigurationSaveMode.Modified);
}
Isn't the program itself authorized to edit its own .config file?
没有。应用程序 运行 所在的用户帐户必须具有写入 folder/file.
的权限
显然它没有适当的权限写入您的“Program Files”文件夹。
当 WPF 程序安装在“Program Files”下时,尝试使用 Configuration.Save()
失败。如果安装在别处,Save()
会按预期工作。程序本身没有被授权编辑自己的 .config 文件吗?
我有两个例外:
System.Configuration: An error occurred loading a configuration file: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied. (C:\Program Files\Advanced Applications\ConfigurationTest\WPF.exe.Config)
mscorlib: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied.
我尝试用 ConfigurationUserLevel.PerUserRoaming)
而不是 ConfigurationUserLevel.None
打开,但它仍然失败,报告了不同的异常:
System.Configuration: An error occurred executing the configuration section handler for appSettings.
System.Configuration: ConfigurationSection properties cannot be edited when locked.
[注意:搜索发现了一篇看似相关但未提供此问题解决方案的文章: Access to path denied for App.Config on Windows 8. How can I update App.Config?
前两个方法从 MainVM 调用并调用底层基本方法,这些方法期望在传入的对象上找到相关 属性,然后同步 AppSettings 中的适当值。
private void SettingsLoad()
{
SettingsLoad(this);
}
private void SettingsSave()
{
try
{
SettingsSave(this);
}
catch (Exception ex)
{
currentMessageAction = MessageAction.Acknowledge;
window.MessagePanel.Show("EXCEPTION!", Utility.ParseException(ex));
}
}
....
public static void SettingsLoad(object main)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
// Attempt to get a reference to the property and, if found, set its value
var prop = main.GetType().GetProperty(setting.Key);
if (prop != null)
{
prop.SetValue(main, setting.Value);
}
}
}
public static void SettingsSave(object main)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
// Attempt to get a reference to the property and, if found, update its value
var prop = main.GetType().GetProperty(setting.Key);
if (prop != null)
{
setting.Value = prop.GetValue(main).ToString();
}
}
// This line produces an exception
config.Save(ConfigurationSaveMode.Modified);
}
Isn't the program itself authorized to edit its own .config file?
没有。应用程序 运行 所在的用户帐户必须具有写入 folder/file.
的权限显然它没有适当的权限写入您的“Program Files”文件夹。