Configuration.Save在网络驱动器上

时间:2015-08-28 07:23:44

标签: c# .net winforms configuration app-config

我为一些简单的任务开发了一个Winforms应用程序。此应用程序有一个App.config文件,其中保存了一些设置。我在应用程序中创建了一个标签页,我可以使用Configuration类调整配置文件的设置。我使用'保存'保存到App.config。应用程序本身中的按钮使用Configuration.Save

但是在保存到网络文件夹上的配置文件时遇到了问题。当我将设置保存到本地文件夹(例如:C:\)上的文件时,一切都很好。我的意思是我将.exe和.config复制到网络文件夹(例如:\\ folder \ application.exe),当我尝试使用Configuration.Save将我的设置保存到配置文件时,我得到了以下错误:

  

System.UnauthorizedAccessException:尝试执行   未经授权的操作。

编辑:或者我可以只使用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()

1 个答案:

答案 0 :(得分: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);
    }