c# – user.config文件没有被创建但是app.config是?

前端之家收集整理的这篇文章主要介绍了c# – user.config文件没有被创建但是app.config是?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的程序内置了一些设置,其中都有一个用户范围.但是,当程序启动时,它只会创建包含设置的AppName.exe.config文件.

在运行时期间稍后保存设置时,它会在AppData / Local / AppName / location中创建user.config文件(以前不存在),但该文件仅包含已保存的设置.

为什么会这样?为什么不创建user.config或在启动时使用它?

解决方法

Application Settings Architecture at MSDN开始:
  1. Application-scoped settings can be stored in either the machine.config or app.exe.config files. Machine.config is always
    read-only,while app.exe.config is restricted by security
    considerations to read-only for most applications.

  2. User-scoped settings can be stored in app.exe.config files,in which case they are treated as static defaults.

  3. Non-default user-scoped settings are stored in a new file,user.config,where user is the user name of the person currently
    executing the application. You can specify a default for a user-scoped
    setting with DefaultSettingValueAttribute. Because user-scoped
    settings often change during application execution,user.config is
    always read/write.

您首先看到的是(您所谓的)您的“内置设置”存储为(Microsoft调用的)“静态默认”用户范围设置,它们存储在app.exe中(按照2).

然后,当您在运行时回写设置时,它们被视为“非默认”用户范围设置,并且它们被写入user.config(按照3),因此为什么只有这样才能看到用户.config文件已创建.

简而言之,只要用户范围设置与每个人相同(默认值),就不需要每用户user.config文件.

原文链接:https://www.f2er.com/csharp/99269.html

猜你在找的C#相关文章