The following code works on Windows XP and 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
But it’s causing an Access to Path denied error on Windows 8. I’ve tried changing the ConfigurationUserLevel to PerUserRoamingAndLocal but it doesn’t do anything.
What do I need to do in order to be able to update the App.Config (or specifically, [application_name].exe.config in Windows 8?
I should add that the user trying running the app has the default Windows 8 privileges. I don’t know exactly what that is, but it’s pretty low. Does this need to be elevated in order for this to work?
Edit:
The logged error:
<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>
Can you post the exception message?
I just tested your code on Win 8 Pro, VS 2012 and it works fine.
yields a config file of
As far as requiring elevated privileges, it depends on which user the application is running under and what the permissions are set to for the file/directory where your config file lives.
If you use the default project location %homepath%\documents\visual studio 2012\projects\, then you would not need to run the application with elevated privileges in order to edit the exe.config file.
If your file lives anywhere else, then you’ll have to take a look at the privileges of that file (right click -> properties -> security tab) .. and see if the user running the application (likely your account) has write privileges.
This is a great explanation on NTFS file permissions.
http://www.techrepublic.com/article/windows-101-know-the-basics-about-ntfs-permissions/6084446