4

I would like my command line program to take file parameters in the form:

-out:%MyDocuments%\dummy.xps

and having %MyDocuments% automatically replaced with:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

I know it's an easy to write function, but I guess it should be already done somewhere, so I don't want to re-invent the wheel.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115

1 Answers1

5

You can use Environment.ExpandEnvironmentVariables(variable);

The input doesn't need to be a single variable, it can be a 'query', like your path, with variables embedded and it will expand all those it finds/recognises. This is shown in the snippet at the link given:

String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
str = Environment.ExpandEnvironmentVariables(query);
Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", Environment.NewLine, str);
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129