문제

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.

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top