以下代码适用于Windows XP和Windows 7:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified); // fails here
但它导致Windows 8上出现“拒绝访问路径”错误。我尝试将ConfigurationUserLevel
更改为PerUserRoamingAndLocal
,但它没有做任何事情。
为了能够在Windows 8中更新App.Config(或者特别是[application_name] .exe.config,我需要做什么?
我应该补充一点,尝试运行该应用的用户具有默认的Windows 8权限。我不确切知道那是什么,但它很低。 是否需要提升才能使其正常工作?
修改 记录的错误:
<Message>Access to the path 'C:\ProgramData\Path\AppName.exe.config' is denied.</Message>
<StackTrace> at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Configuration.Internal.WriteFileContext.ValidateWriteAccess(String filename)
at System.Configuration.Internal.WriteFileContext.Complete(String filename, Boolean success)
at System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.UpdateConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save(ConfigurationSaveMode saveMode)
at MyNamespace.Infrastructure.ConfigurationManager.WriteToAppSettings(String key, String value) in C:\Path\To\App\AppName\Infrastructure\ConfigurationManager.cs:line 76
</StackTrace>
答案 0 :(得分:4)
您可以发布异常消息吗?
我刚刚在Win 8 Pro,VS 2012上测试了你的代码,它运行良好。
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("my_new_key");
config.AppSettings.Settings.Add("my_new_key", "my new value");
config.Save(ConfigurationSaveMode.Modified);
产生
的配置文件<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="my_new_key" value="my new value" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
至于需要提升权限,它取决于运行应用程序的用户以及配置文件所在的文件/目录的权限设置。
如果使用默认项目位置%homepath%\ documents \ visual studio 2012 \ projects \,则无需使用提升的权限运行应用程序即可编辑exe.config文件。
如果您的文件位于其他任何位置,那么您必须查看该文件的权限(右键单击 - &gt;属性 - >安全选项卡)..并查看用户是否正在运行该应用程序(可能您的帐户)具有写权限。
这是对NTFS文件权限的一个很好的解释。 http://www.techrepublic.com/article/windows-101-know-the-basics-about-ntfs-permissions/6084446
答案 1 :(得分:1)
尝试此操作以删除密钥:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings.Remove("ConnectionString");
appSettings.Settings.Add("ConnectionString", "");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
或更改键值:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;
settings["KeyName"].Value = "newkeyvalue";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);