gpt4 book ai didi

c# - 保存自定义配置部分会导致 System.UnauthorizedAccessException

转载 作者:太空狗 更新时间:2023-10-29 23:52:09 26 4
gpt4 key购买 nike

我在尝试使用我的 app.config 中的自定义配置部分时遇到以下问题。我使用自定义配置部分来跟踪我想用我的程序备份的文件夹选择,如下所示:

<CustomConfigSection>
<BackupLocations>
<clear />
<add path="C:\Users\Marcel\Documents\" />
</BackupLocations>
</CustomConfigSection>

现在,每当我保存配置文件时,我都会得到这个异常:

System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Access to the path C: \ Program Files (x86) \ Backup Solutions \ uqhuxi1j.tmp is denied. (C: \ Program Files (x86) \ Backup Solutions \ BS.exe.Config) ---> System.UnauthorizedAccessException

我使用的代码是:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
CustomConfigSection section = (CustomConfigSection)config.GetSection("CustomConfigSection");

section.BackupLocations.Add(element);

section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);

这在以管理员身份登录时工作正常,并且仅在 PC 上配置了多个帐户时才会发生,因此这意味着这是一个 UAC/权限问题。作为普通用户,我没有该文件夹的正确写入权限。

现在回答我的问题:

为什么用户设置会保存到 AppData 而自定义配置部分却试图保存在 ApplicationFolder/exe.config 中?如何将我的自定义部分也保存到 AppData 配置文件?

我需要能够为每个用户保存我的数据,无论权限如何。那么我可以使用自定义配置部分实现这一点,还是需要不同的方法?最好我也想将我的自定义部分保存到 AppData 配置文件中。我不想在我的安装程序中使用技巧来调整我的应用程序文件夹的权限以允许它写入。 最重要的是,我不想在启动我的程序时要求管理员权限!

感谢您的回复,非常感谢。

最佳答案

用户范围的设置被保存到 AppData,因为用户有权在那里写入。用户无权写入程序文件。使用用户范围的设置以将它们保存到 AppData。参见 http://msdn.microsoft.com/en-us/library/a65txexh(v=vs.100).aspx了解更多详情。

关于c# - 保存自定义配置部分会导致 System.UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com