Skip to content

Instantly share code, notes, and snippets.

@kenzauros
Last active January 24, 2019 01:19
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 kenzauros/127be6447fb8d5a135a1d6f39f948795 to your computer and use it in GitHub Desktop.
Save kenzauros/127be6447fb8d5a135a1d6f39f948795 to your computer and use it in GitHub Desktop.
DotNetEnv を使った .NET アプリでの環境変数管理用ヘルパークラス
using System.Runtime.CompilerServices;
/// <summary>
/// 環境変数を管理します。
/// </summary>
public static class Env
{
/// <summary>
/// デフォルトの .env ファイルのファイル名
/// </summary>
public const string DEFAULT_ENV_FILENAME = ".env";
/// <summary>
/// 環境変数を .env ファイルから読み込みます。
/// </summary>
/// <param name="path"></param>
public static void Load(string path = null)
{
DotNetEnv.Env.Load(path ?? DEFAULT_ENV_FILENAME);
}
#region 環境変数プロパティからの呼び出し用プロキシメソッド
public static string GetString([CallerMemberName] string key = "") => DotNetEnv.Env.GetString(key);
public static int GetInt([CallerMemberName] string key = "") => DotNetEnv.Env.GetInt(key);
public static bool GetBool([CallerMemberName] string key = "") => DotNetEnv.Env.GetBool(key);
public static double GetDouble([CallerMemberName] string key = "") => DotNetEnv.Env.GetDouble(key);
#endregion
#region 環境変数プロパティ
// サンプル
public static string FILE_PATH => GetString();
public static int EXPIRATION_PERIOD => GetInt();
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment