https://github.com/nozzlegear/dotenvfile
A .NET library for parsing .env files and optionally injecting their variables into the environment
https://github.com/nozzlegear/dotenvfile
Last synced: 6 months ago
JSON representation
A .NET library for parsing .env files and optionally injecting their variables into the environment
- Host: GitHub
- URL: https://github.com/nozzlegear/dotenvfile
- Owner: nozzlegear
- License: mit
- Created: 2016-08-04T21:58:35.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-02-28T16:02:32.000Z (over 5 years ago)
- Last Synced: 2025-03-27T20:12:09.720Z (6 months ago)
- Language: F#
- Size: 233 KB
- Stars: 3
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DotEnvFile
[](https://travis-ci.org/nozzlegear/dotenvfile)
[](https://www.nuget.org/packages/dotenvfile/)
[](https://github.com/nozzlegear/dotenvfile/blob/master/LICENSE)DotEnvFile is a small .NET utility for parsing environment variables from `.env` files and optionally injecting them into the current environment. It supports .NET Core and .NET Standard. While this package is written in F#, it's completely usable in C# without knowing _anything_ about the F# language.
## Installation
You can download DotEnvFile from Nuget. Use the dotnet CLI to install it:
```sh
dotnet add package dotenvfile
```Or via Nuget package manager in Visual Studio:
```bash
Install-Package DotEnvFile
```Or via [Paket](https://github.com/fsprojects/paket) for F# projects:
```bash
paket add nuget DotEnvFile
```## File formatting
DotEnvFile expects your file to contain one variable per line, with each variable formatted in the scheme `Key:Value`, `Key=Value`, or `Key Value`. It will try to be forgiving by ignoring extra whitespace and empty lines.
Example `.env` file:
```
FirstKey: FirstValue
SecondKey= SecondValue
ThirdKey ThirdValue
FourthKey : FourthValue
```## Usage
### Load environment variables from your file:
This function accepts a boolean that tells the tool whether it should throw an exception when it encounters a value it can't parse, or if it should skip the line and continue silently.
```cs
string pathToFile = "/path/to/file.env";
bool throwOnFormatException = true;
IDictionary variables = DotEnvFile.LoadFile(pathToFile, throwOnFormatException);
```### Parse a single line:
```cs
KeyValuePair variable = DotEnvFile.ParseLine("MyKey=MyValue");
```### Inject the variables into your environment:
```cs
DotEnvFile.InjectIntoEnvironment(System.EnvironmentVariableTarget.Process, variables);Console.WriteLine(Environment.GetEnvironmentVariable("MyKey")); // "MyValue"
```### Remove the variables from your environment:
```cs
DotEnvFile.RemoveFromEnvironment(System.EnvironmentVariableTarget.Process, variables);Console.WriteLine(Environment.GetEnvironmentVariable("MyKey") == null); // True
```