Environment.ExpandEnvironmentVariables(String) 方法

定义

将嵌入到指定字符串中的每个环境变量的名称替换为该变量的值的等效字符串,然后返回结果字符串。

public:
 static System::String ^ ExpandEnvironmentVariables(System::String ^ name);
public static string ExpandEnvironmentVariables (string name);
static member ExpandEnvironmentVariables : string -> string
Public Shared Function ExpandEnvironmentVariables (name As String) As String

参数

name
String

包含零个或多个环境变量名的字符串。 每个环境变量都用百分号 (%) 引起来。

返回

String

一个字符串,其中的每个环境变量均被替换为该变量的值。

例外

name 上声明的默认值为 null

示例

以下示例演示如何获取系统驱动器和系统根变量。

// Sample for the Environment::ExpandEnvironmentVariables method
using namespace System;
int main()
{
   String^ str;
   String^ nl = Environment::NewLine;
   Console::WriteLine();
   
   //  <-- Keep this information secure! -->
   String^ query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
   str = Environment::ExpandEnvironmentVariables( query );
   Console::WriteLine( "ExpandEnvironmentVariables: {0} {1}", nl, str );
}

/*
This example produces the following results:

ExpandEnvironmentVariables:
My system drive is C: and my system root is C:\WINNT
*/
// Sample for the Environment.ExpandEnvironmentVariables method
using System;

class Sample
{
    public static void Main()
    {
        // Keep this information secure!
        string query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";

        string str = Environment.ExpandEnvironmentVariables(query);

        Console.WriteLine(str);
    }
}
/*
This example prints:

My system drive is C: and my system root is C:\WINDOWS
*/
// Sample for the Environment.ExpandEnvironmentVariables method
open System

let nl = Environment.NewLine

//  <-- Keep this information secure! -->
let query = "My system drive is %SystemDrive% and my system root is %SystemRoot%"
let str = Environment.ExpandEnvironmentVariables query
printfn $"\nExpandEnvironmentVariables: {nl}  {str}"

// This example produces the following results:
//     ExpandEnvironmentVariables:
//       My system drive is C: and my system root is C:\WINNT
' Sample for the Environment.ExpandEnvironmentVariables method
Class Sample
   Public Shared Sub Main()
      Dim str As [String]
      Dim nl As [String] = Environment.NewLine
      
      Console.WriteLine()
      '  <-- Keep this information secure! -->
      Dim query As [String] = "My system drive is %SystemDrive% and" & _ 
                              "my system root is %SystemRoot%"
      str = Environment.ExpandEnvironmentVariables(query)
      Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str)
   End Sub
End Class
'
'This example produces the following results:
'
'ExpandEnvironmentVariables:
'  My system drive is C: and my system root is C:\WINNT
'

注解

COM 互操作用于从操作系统检索环境变量。 如果由于 COM 错误而无法检索环境变量,则解释失败原因的 HRESULT 用于生成多个可能的异常之一:也就是说,异常取决于 HRESULT。 有关如何处理 HRESULT 的详细信息,请参阅方法的 Marshal.ThrowExceptionForHR “备注”部分。

仅针对设置的环境变量进行替换。 例如,假设 name 为“MyENV = %MyENV%”。 如果环境变量 MyENV 设置为 42,则此方法返回“MyENV = 42”。 如果未设置 MyENV,则不会发生更改;此方法返回“MyENV = %MyENV%”。

返回值的大小限制为 32K。

适用于