ConfigurationManager.save() fails

倾然丶 夕夏残阳落幕 提交于 2019-12-12 11:35:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!