我正在.NET 4中创建一个应用程序,它需要在应用程序运行时更新应用程序的配置。执行此操作的代码如下:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["MySetting"].Value = "my value";
config.Save();
我的App.Config看起来像这样......
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MySetting" value="my old value"/>
</appSettings>
</configuration>
当我在本地驱动器上运行此应用程序时(因此在运行程序时配置文件也将位于本地驱动器上),此代码可以正常工作。但是,如果应用程序使用网络共享上的配置文件(实际上是Z :)运行,那么代码将失败并在config.Save();
行上出现以下异常......
System.InvalidOperationException was unhandled Message=Method failed with unexpected error code 1. Source=mscorlib StackTrace: 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.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory) 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.DuplicateFileAttributes(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, 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() at MyProj.Program.Main() in Z:\repos\project\MyProj\Program.cs:line 61 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) at System.Activator.CreateInstance(ActivationContext activationContext) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
我认为这是.NET的某种安全问题,但我不确定如何禁用该安全性。
存储配置文件的网络共享实际上是VirtualBox共享驱动器。主机系统是linux,共享驱动器映射到Ext4文件系统。
我不相信这只是一个文件系统写入问题,因为我可以成功写入与App.config文件位于同一目录中的文本文件(至少我认为它是同一个目录,除非.NET正在执行幕后花哨的东西,如使用临时目录)。
File.WriteAllText("Text.txt", "Testing");
答案 0 :(得分:0)
这看起来像代码访问安全问题,这里有一个很好的帖子:http://www.sellsbrothers.com/Posts/Details/1519
基本上,当您从网络驱动器运行应用程序时,默认情况下,它的信任级别为&#34; Intranet&#34;我认为不允许你修改配置文件。
所以基本上你需要通过这样的方式提升该驱动器的信任级别:
caspol -q -machine -addgroup 1 -url file://z:/* FullTrust -name "Z Drive"
希望有帮助,我无法访问网络驱动器,因此无法确认。
答案 1 :(得分:0)
我尝试了caspol
并使用.Net 4,但没有成功。这可能很愚蠢,但是我只能设法做到这一点:
APP_CONFIG_FILE
并将其存储在本地变量中APP_CONFIG_FILE
APP_CONFIG_FILE
APP_CONFIG_FILE
设置为网络上的原始文件例如:
string APP_CONFIG_FILE = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
string TEMP_FILE = System.IO.Path.GetTempPath() + Guid.NewGuid();
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", TEMP_FILE);
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["MySetting"].Value = "my value";
config.Save();
File.Copy(TEMP_FILE, APP_CONFIG_FILE, true);
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", APP_CONFIG_FILE);
File.Delete(TEMP_FILE);
有趣的是,当我在装有Visual Studio的计算机上运行该程序时,它运行良好(无需使用caspol
),而无需使用上述步骤。