Skip to content

Instantly share code, notes, and snippets.

@jsancho
Last active November 30, 2021 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsancho/eedbd81d6a33fa9e60fd09a140e9e5fe to your computer and use it in GitHub Desktop.
Save jsancho/eedbd81d6a33fa9e60fd09a140e9e5fe to your computer and use it in GitHub Desktop.
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