如何在.NET中扩展环境变量%CommonProgramFiles%\ system \

时间:2022-09-24 23:29:14

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?

如何在.net代码中解析此类目录路径?

2 个解决方案

#1


26  

Environment.ExpandEnvironmentVariables. If you control the creation of the registry value, store it as an expandable string in the registry and the registry API will automatically expand it for you.

Environment.ExpandEnvironmentVariables。如果您控制注册表值的创建,请将其存储为注册表中的可扩展字符串,注册表API将自动为您展开它。

#2


18  

You can use the Environment.GetEnvironmentVariable function:

您可以使用Environment.GetEnvironmentVariable函数:

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

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

然后,您可以使用Path.Combine追加路径的其余部分:

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

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

另一种选择是使用Environment.ExpandEnvironmentVariables。这将用它们的值替换所有环境变量:

string fullPath = Environment.ExpandEnvironmentVariables("%CommonProgramFiles%\System\web32.dll");

#1


26  

Environment.ExpandEnvironmentVariables. If you control the creation of the registry value, store it as an expandable string in the registry and the registry API will automatically expand it for you.

Environment.ExpandEnvironmentVariables。如果您控制注册表值的创建,请将其存储为注册表中的可扩展字符串,注册表API将自动为您展开它。

#2


18  

You can use the Environment.GetEnvironmentVariable function:

您可以使用Environment.GetEnvironmentVariable函数:

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

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

然后,您可以使用Path.Combine追加路径的其余部分:

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

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

另一种选择是使用Environment.ExpandEnvironmentVariables。这将用它们的值替换所有环境变量:

string fullPath = Environment.ExpandEnvironmentVariables("%CommonProgramFiles%\System\web32.dll");