Configuration.Save 在网络驱动器上

Configuration.Save on a network drive

我为一些简单的任务开发了一个 Winforms 应用程序。此应用程序有一个 App.config 文件,其中保存了一些设置。我在应用程序中创建了一个标签页,我可以在其中使用 Configuration class. I save to the App.config using a 'Save' button in the application itself which uses the Configuration.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()

该文件可能是以管理员身份在该位置创建的,这将使其无法被任何其他非管理员用户使用。

您需要设置文件权限以允许所有人都喜欢这样

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