ConfigurationManager.save() 失败
ConfigurationManager.save() fails
我正在编写一个 C# 应用程序(没有 WinForms)。
阅读了几个关于 ConfigurationManager 的线程后,我以以下代码结束:
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() 时遇到此异常; :
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
这是怎么回事,我该如何解决?
根据 GrawCubes 的评论,事实证明该文件夹是一个 VirtualBox 共享文件夹。
我正在编写一个 C# 应用程序(没有 WinForms)。
阅读了几个关于 ConfigurationManager 的线程后,我以以下代码结束:
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() 时遇到此异常; :
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
这是怎么回事,我该如何解决?
根据 GrawCubes 的评论,事实证明该文件夹是一个 VirtualBox 共享文件夹。