机器级别和用户级别有没有`appsettings.json`配置文件?
Is there an `appsettings.json` configuration file at the machine level and user levels?
我知道,对于桌面应用程序,旧的 *.config
机制有四个级别的配置文件:
machine.config
- 在 .net 安装目录中
app.config
- 在应用程序的目录中
user.config
- 在用户的漫游目录中
user.config
- 在用户的本地目录
新系统中那些文件的等价物是什么,即使用 JSON 个文件的系统?
您可以使用 ConfigurationBuilder
精细地完成所有这些工作,它可以灵活地处理所有这些问题,并且只需按正确的顺序添加已知的配置文件即可。缺点是它有点冗长。但是,如果需要,请自己编写一个扩展方法:
注意:如果您使用的是asp.netcore,您可以访问配置方法中的IConfigurationRoot
例如 ConfigureAppConfiguration
,因此您不需要实例化一个新的 ConfigurationBuilder
,如下所示。
var configuration = new ConfigurationBuilder()
.SetBasePath(...)
// some machine config
.AddJsonFile(@"<The path to your globalSettings>\appsettings.json", optional: true)
// Your main app settings
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
// environment specific
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
// Machine specific
.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true)
// user specific
.AddJsonFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Your App Name","appsettings.json"), optional: true)
.AddEnvironmentVariables()
...
免责声明 : 以下内容不代表世界上最伟大的代码,仅供参考。您需要做任何对您和您的环境有意义的事情。
长话短说:
IConfigurationRoot
只是提供者和来源的集合。提供者折叠成键值对的集合。您添加的每个后续提供程序都能够覆盖以前的提供程序值(如果可用),并且比旧的 app.config
/web.config
范例好大约 1000 倍。
我知道,对于桌面应用程序,旧的 *.config
机制有四个级别的配置文件:
machine.config
- 在 .net 安装目录中app.config
- 在应用程序的目录中user.config
- 在用户的漫游目录中user.config
- 在用户的本地目录
新系统中那些文件的等价物是什么,即使用 JSON 个文件的系统?
您可以使用 ConfigurationBuilder
精细地完成所有这些工作,它可以灵活地处理所有这些问题,并且只需按正确的顺序添加已知的配置文件即可。缺点是它有点冗长。但是,如果需要,请自己编写一个扩展方法:
注意:如果您使用的是asp.netcore,您可以访问配置方法中的IConfigurationRoot
例如 ConfigureAppConfiguration
,因此您不需要实例化一个新的 ConfigurationBuilder
,如下所示。
var configuration = new ConfigurationBuilder()
.SetBasePath(...)
// some machine config
.AddJsonFile(@"<The path to your globalSettings>\appsettings.json", optional: true)
// Your main app settings
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
// environment specific
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
// Machine specific
.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true)
// user specific
.AddJsonFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Your App Name","appsettings.json"), optional: true)
.AddEnvironmentVariables()
...
免责声明 : 以下内容不代表世界上最伟大的代码,仅供参考。您需要做任何对您和您的环境有意义的事情。
长话短说:
IConfigurationRoot
只是提供者和来源的集合。提供者折叠成键值对的集合。您添加的每个后续提供程序都能够覆盖以前的提供程序值(如果可用),并且比旧的 app.config
/web.config
范例好大约 1000 倍。