ConfigurationManager.save()失败 [英] ConfigurationManager.save() fails

查看:263
本文介绍了ConfigurationManager.save()失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C#应用程序(没有的WinForms)

I am writing a C# application(no WinForms).

看了约ConfigurationManager中多个线程我有下面的代码结束:

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);
    }
}

当我打电话我得到这个例外congig.save ();

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?

推荐答案

以下GrawCubes此话事实证明这是一个事实,即该文件夹是VirtualBox的共享文件夹。

Following GrawCubes remark it turns out it was the fact that the folder was a VirtualBox Shared Folder.

这篇关于ConfigurationManager.save()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆