Apq.Cfg.Env
1.2.1
dotnet add package Apq.Cfg.Env --version 1.2.1
NuGet\Install-Package Apq.Cfg.Env -Version 1.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Apq.Cfg.Env" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Apq.Cfg.Env" Version="1.2.1" />
<PackageReference Include="Apq.Cfg.Env" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Apq.Cfg.Env --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Apq.Cfg.Env, 1.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Apq.Cfg.Env@1.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Apq.Cfg.Env&version=1.2.1
#tool nuget:?package=Apq.Cfg.Env&version=1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Apq.Cfg.Env
.env 文件配置源扩展包。
仓库地址:https://gitee.com/apq/Apq.Cfg
📖 在线文档:https://apq-cfg.vercel.app/
依赖
- Apq.Cfg
默认层级
该配置源的默认层级为 CfgSourceLevels.Env (400)。
如果不指定 level 参数,将使用默认层级:
// 使用默认层级 400
.AddEnvFile(".env")
// 指定自定义层级
.AddEnvFile(".env", level: 450)
用法
using Apq.Cfg;
using Apq.Cfg.Env;
var cfg = new CfgBuilder()
.AddEnvFile(".env", level: 0, writeable: true)
.Build();
// 使用索引器访问(__ 会自动转换为 :)
var dbHost = cfg["DATABASE:HOST"];
// 使用配置节
var db = cfg.GetSection("DATABASE");
var host = db["HOST"];
var port = db.GetValue<int>("PORT");
方法签名
public static CfgBuilder AddEnv(
this CfgBuilder builder,
string path,
int level = CfgSourceLevels.Env, // 默认 400
bool writeable = false,
bool optional = true,
bool reloadOnChange = true,
bool isPrimaryWriter = false,
bool setEnvironmentVariables = false)
参数说明
| 参数 | 说明 | 默认值 |
|---|---|---|
path |
.env 文件路径 | - |
level |
配置层级,数值越大优先级越高 | 400 |
writeable |
是否可写 | false |
optional |
文件不存在时是否忽略 | true |
reloadOnChange |
文件变更时是否自动重载 | true |
isPrimaryWriter |
是否为默认写入目标 | false |
setEnvironmentVariables |
是否将配置写入系统环境变量 | false |
.env 格式示例
# 这是注释
APP_NAME=MyApp
APP_DEBUG=true
# 数据库配置(使用 __ 表示嵌套)
DATABASE__HOST=localhost
DATABASE__PORT=5432
DATABASE__NAME=mydb
# 支持引号包裹的值
MESSAGE="Hello, World!"
MULTILINE="Line1\nLine2"
# 支持单引号(不处理转义)
RAW_VALUE='Hello\nWorld'
# 支持 export 前缀
export API_KEY=secret123
配置键映射
.env 文件使用双下划线 __ 来表示配置层级,读取时会自动转换为 ::
| .env 键 | 配置键 |
|---|---|
APP_NAME |
APP_NAME |
DATABASE__HOST |
DATABASE:HOST |
DATABASE__CONNECTION__STRING |
DATABASE:CONNECTION:STRING |
支持的特性
- ✅ 注释(以
#开头) - ✅ 双引号包裹的值(支持转义字符)
- ✅ 单引号包裹的值(原样保留)
- ✅
export前缀 - ✅ 嵌套配置(使用
__) - ✅ 文件变更自动重载
- ✅ 可写配置源
- ✅ 写入系统环境变量
写入系统环境变量
默认情况下,.env 文件中的配置只会加载到配置系统中。如果需要将配置同时写入系统环境变量(例如供子进程使用),可以启用 setEnvironmentVariables 参数:
var cfg = new CfgBuilder()
.AddEnvFile(".env", level: 0, writeable: false, setEnvironmentVariables: true)
.Build();
// .env 文件中的 DATABASE__HOST=localhost 会:
// 1. 作为配置键 "DATABASE:HOST" 可通过 cfg["DATABASE:HOST"] 访问
// 2. 同时设置系统环境变量 DATABASE__HOST=localhost
注意:启用此选项后,配置会写入当前进程的环境变量,子进程可以继承这些环境变量。但这不会影响系统级别的环境变量。
许可证
MIT License
作者
- 邮箱:amwpfiqvy@163.com
仓库
- Gitee:https://gitee.com/apq/Apq.Cfg
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.