Skip to content

Instantly share code, notes, and snippets.

@bestpika
Last active October 19, 2023 08:44
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 bestpika/576fe7b96288965a242c2207069e61ce to your computer and use it in GitHub Desktop.
Save bestpika/576fe7b96288965a242c2207069e61ce to your computer and use it in GitHub Desktop.
using Microsoft.Extensions.Configuration;
public class AppSettings
{
static private readonly IConfiguration _config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddYamlFile("appsettings.yaml", optional: false, reloadOnChange: true)
.Build();
public static string? ConnectionString(string name = "DefaultConnection") => _config.GetConnectionString(name);
public static string? GetSection(string path) => _config.GetSection(path).Value;
public static T? GetValue<T>(string path) => _config.GetValue<T>(path);
public static string? GetValue(string path) => _config.GetValue<string>(path);
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>MailSender</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<None Include="appsettings.yaml" CopyToOutputDirectory="Always" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment