Alex Rivera | Logout

How to expand environment variable %CommonProgramFiles%\system\ in .NET

Asked 2010-04-25T07:03:08.137
28

I have a situation where I need to return a directory path by reading the registry settings. Registry value returns me a path in the format

%CommonProgramFiles%\System\web32.dll

while the consumer code is expecting it in the format

C:\Program Files\Common Files\System\web32.dll

How can I resolve such directory path in .net code?

Edit
Report

1 Answer

23

You can use the Environment.GetEnvironmentVariable function:

string commonDir = Environment.GetEnvironmentVariable("CommonProgramFiles");

You can then use Path.Combine to append the rest of the path:

string fullPath = Path.Combine(commonDir, "System", "web32.dll");

Another option is to use Environment.ExpandEnvironmentVariables. This will replace all environment variables with their values:

string fullPath = Environment.ExpandEnvironmentVariables("%CommonProgramFiles%\System\web32.dll");
answered 2010-04-25T07:07:25.883

Your Answer