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
Thanks in advance.
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).
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.