【问题标题】:Configuration.Save on a network drive配置。保存在网络驱动器上
【发布时间】:2015-08-28 20:54:44
【问题描述】:

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

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

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

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

编辑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()

【问题讨论】:

  • 我曾经做过这样的事情,但从未见过这个问题。我在文件中有用户设置和应用程序设置 - 但是当我保存它时总是在 AppData 文件夹中。这有堆栈跟踪吗?
  • 为什么要保存在 AppData 文件夹中?我正在使用的 .config 文件始终与 .exe 一起使用,因此在网络驱动器本身中而不是在 AppData 中。我认为这就是你和我的环境之间的差异
  • 我也遇到了同样的问题,请问你是怎么解决的?

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


【解决方案1】:

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

你需要设置文件权限,让每个人都这样

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

【讨论】:

  • XmlSettingsManager?这是否包含在 .NET Framework 中?但是,我可以应用我认为的访问控制,但它不会改变任何行为。我不断收到错误
  • 抱歉,忘记提及您需要删除文件并让应用程序在标准凭据下创建它,然后才能再次运行它
  • 对不起,那是来自我的自定义类,设置文件可以使用 XDocument.Load(...) 读取
猜你喜欢
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多