Last active
November 30, 2021 12:20
-
-
Save jsancho/eedbd81d6a33fa9e60fd09a140e9e5fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace YourNamespace | |
{ | |
// adapted from https://dusted.codes/dotenv-in-dotnet | |
public static class DotEnv | |
{ | |
public static void Load() | |
{ | |
var currentDir = Directory.GetCurrentDirectory(); | |
var root = Directory.GetParent(currentDir).Parent.Parent.FullName; | |
var dotenv = Path.Combine(root, ".env"); | |
Load(dotenv); | |
} | |
public static void Load(string filePath) | |
{ | |
if (!File.Exists(filePath)) | |
return; | |
foreach (var line in File.ReadAllLines(filePath)) | |
{ | |
var parts = line.Split('=', 2, | |
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | |
if (parts.Length != 2) | |
continue; | |
Environment.SetEnvironmentVariable(parts[0], parts[1]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment