Coder Social home page Coder Social logo

dotnet-env's Introduction

Windows build status License: MIT NuGet version

dotnet-env

A .NET library to load environment variables from .env files. Supports .NET Core and .NET Framework

Installation

Available on NuGet

Visual Studio:

PM> Install-Package DotNetEnv
PM> Install-Package DotNetEnv.AspNetCore #if you're working on an asp.net core project

.NET Core CLI:

dotnet add package DotNetEnv
dotnet add package DotNetEnv.AspNetCore #if you're working on an asp.net core project

Usage

Load env file

Will automatically look for a .env file in the current directory

DotNetEnv.Env.Load();

You can also specify the path to the .env file

DotNetEnv.Env.Load("./path/to/.env");

The variables in the .env can then be accessed through the System.Environment class

System.Environment.GetEnvironmentVariable("IP")

Usage (with ASP.NET Core)

In your Startup.cs file, add the following imports:

using DotNetEnv;
using DotNetEnv.AspNetCore;

In your Configure method, add the following:

app.UseDotNetEnv(); // to load the variables from the .env file

Additional arguments

You can also control whitespace trimming and allowing hashes in values

DotNetEnv.Env.Load(false, false);

Both parameters default to true, which means:

  1. trimWhitespace, first arg: true in order to trim leading and trailing whitespace from keys and values such that
  KEY  =  value

Would then be available as

"value" == System.Environment.GetEnvironmentVariable("KEY")
null == System.Environment.GetEnvironmentVariable("  KEY  ")

False would mean:

"  value" == System.Environment.GetEnvironmentVariable("  KEY  ")
null == System.Environment.GetEnvironmentVariable("KEY")
  1. isEmbeddedHashComment, second arg: true in order to allow inline comments
KEY=value  # comment

Would then be available as

"value" == System.Environment.GetEnvironmentVariable("KEY")

False would mean:

"value  # comment" == System.Environment.GetEnvironmentVariable("KEY")

Which is most useful when you want to do something like:

KEY=value#moreValue#otherValue#etc

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

dotnet-env's People

Contributors

tonerdo avatar mykeels avatar forhot2000 avatar rogusdev avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.