Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top