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

后端 未结 2 526
误落风尘
误落风尘 2021-02-12 05:47

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%\         


        
相关标签:
2条回答
  • 2021-02-12 06:15

    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");
    
    0 讨论(0)
  • 2021-02-12 06:16

    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.

    0 讨论(0)
提交回复
热议问题