Search code examples
c#.netenvironment-variablesconfiguration-filesvariable-substitution

How to expand environment variables with substitutions?


Goal

We are using environment variables like %logonserver% and %userdomain% in our .NET application's configuration files, to support configuration freedom to application managers. When parsing the configuration file a call to Environment.ExpandEnvironmentVariables resolves the actual value.

Problem

However it seems it can not expand environment variables with substitutions like:

  • %logonserver:~2% (skips the first two characters)
  • %logonserver:\\=% (replaces the backslashes with an empty string)

When I call Environment.ExpandEnvironmentVariables("%logonserver:~2%") the method just returns the same string: %logonserver:~2%, instead of the expanded and substituted variable like a command line call would: echo %logonserver:~2%.

Questions

  1. Is there anything I'm doing wrong?
  2. What's an alternative to accomplish this goal in .net?

Thanks in advance.


Solution

    1. You make nothing wrong. All the stuff after the colon : is a special feature of the command shell that won't be mimic by .Net or the Win32 API (simply check this code).

    2. There are two possibilities:

      • Take the string till the :, add a percent sign % and give this to ExpandEnvironmentVariables(). Then write your own parser to apply the needed actions after the colon : to be done on the returned string to mimic the behavior of the console.

      • Start a process with a hidden console window, take its output and input streams and send your environments variables with echo commands to it and let the parsing be done by the console.