问题
I am writing a C# application(no WinForms).
Having read several threads about ConfigurationManager i have ended with the following code:
public static string GetValue(string key)
{
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
throw new ArgumentException("Requested unknown key from Application Configuration("+configFile.FilePath+")",key);
System.Console.Out.WriteLine("Got "+ key + " = " + settings[key].Value);
return settings[key].Value;
}
public static void SetValue(string key, string value)
{
try
{
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
settings.Remove(key);
settings.Add(key, value);
configFile.Save(ConfigurationSaveMode.Modified,true);
}
catch (Exception e)
{
Console.WriteLine("Error writing app settings:"+e.Message+e.StackTrace);
}
}
I am getting this exception when i call congig.save(); :
Error writing app settings: Method failed with unexpected error code 1. at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity..ctor(Boolean isContainer, ResourceType resourceType, String name, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.FileSecurity..ctor(String fileName, AccessControlSections includeSections)
at System.Configuration.Internal.WriteFileContext.DuplicateTemplateAttributes(String source, String destination)
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)
at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
at AppConfig.SetValue(String key, String value) in e:\FsLink\ConduitCore\AppConfig.cs:line 37
What is happening and how can i fix it?
回答1:
Following GrawCubes remark it turns out it was the fact that the folder was a VirtualBox Shared Folder.
来源:https://stackoverflow.com/questions/29120361/configurationmanager-save-fails