Configuration.Save在网络驱动器上

问题描述:

我为一些简单任务开发了Winforms应用程序。此应用程序具有保存一些设置的App.config文件。我在应用程序中创建了一个标签页,我可以使用Configuration类来调整配置文件的设置。我使用应用程序本身中的“保存”按钮保存到App.config中,该应用程序使用Configuration.SaveConfiguration.Save在网络驱动器上

但我在保存到网络文件夹的配置文件时遇到了麻烦。当我将设置保存到本地文件夹中的文件(如:C:\)时,一切正常。我的意思是我将.exe和.config复制到网络文件夹(例如:\\ folder \ application.exe),当我尝试使用Configuration.Save将设置保存到配置文件时,出现以下错误:

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.

编辑:或者我可以只使用StreamWriter而不是配置?

编辑2:

这是堆栈跟踪:

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. 
    at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl) 
    at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext) 
    at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext) 
    at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath) 
    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) 
    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 ToolNameSpace.Configs.Save() 
+0

我用来做这样的事情,但从来没有见过这个问题。我在文件中有用户设置和应用程序设置 - 但是当我保存它时总是到AppData文件夹。有没有一个堆栈跟踪? – Andez

+1

你为什么要保存在AppData文件夹中?我使用的.config文件总是与.exe一起,所以在网络驱动器本身中,而不是在AppData中。这是你的环境和我的区别我认为 – Ozkan

+0

我面临同样的问题,请让我知道你是如何解决它? –

该文件可能已在该位置作为管理员这将使其无法使用任何其他非管理用户创建。

你需要设置文件权限让大家喜欢这个

private void CreateSettingsFile(string path) 
    { 
     XDocument document = new XDocument(); 
     XElement rootElement = new XElement("settings"); 
     document.Add(rootElement); 
     document.Save(path); 
     var accessControl = File.GetAccessControl(path); 
     var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); 
     accessControl.AddAccessRule(new FileSystemAccessRule(everyone, 
                  FileSystemRights.FullControl | 
                  FileSystemRights.Synchronize, 
                  AccessControlType.Allow)); 

     manager = new XmlSettingsManager(document,() => XDocument.Load("file://" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.settingsPath)), 
      x => x.Save(this.settingsPath)); 
     File.SetAccessControl(path, accessControl); 
    } 
+0

XmlSettingsManager?这是否包含在.NET Framework中?但是,我可以应用我认为的访问控制,但它不会改变任何行为。我不断收到错误 – Ozkan

+0

抱歉,忘了提及您需要删除该文件,并让应用程序在标准凭据下创建它,然后才能再次运行它 –

+0

对不起,那是从我的自定义类中,设置文件可以是使用XDocument.Load(...)读取 –