Skip to content

Instantly share code, notes, and snippets.

@angularsen
Created March 19, 2016 11:11
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 angularsen/4e7232c856e8020b0e0b to your computer and use it in GitHub Desktop.
Save angularsen/4e7232c856e8020b0e0b to your computer and use it in GitHub Desktop.
Get web/app.config setting and expand environment variables when developing locally
using System;
using System.Collections.Specialized;
using JetBrains.Annotations;
namespace MyExtensions
{
public static class NameValueCollectionExtensions
{
[CanBeNull]
public static string GetWithEnv([NotNull] this NameValueCollection nvc, [NotNull] string name)
{
if (nvc == null) throw new ArgumentNullException(nameof(nvc));
if (name == null) throw new ArgumentNullException(nameof(name));
string value = nvc.Get(name);
if (value != null)
value = Environment.ExpandEnvironmentVariables(value);
return value;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="SomeApiUserName" value="%myapp_someapi_username%" />
<add key="SomeApiPassword" value="%myapp_someapi_password%" />
</appsettings>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment