Environment.ExpandEnvironmentVariables(String) 方法

定义

将嵌入到指定字符串中的每个环境变量的名称替换为该变量的值的等效字符串,然后返回结果字符串。Replaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting 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

包含零个或多个环境变量名的字符串。A string containing the names of zero or more environment variables. 每个环境变量都用百分号 (%) 引起来。Each environment variable is quoted with the percent sign character (%).

返回

一个字符串,其中的每个环境变量均被替换为该变量的值。A string with each environment variable replaced by its value.

例外

namenullname is null.

示例

下面的示例演示如何获取系统驱动器和系统根变量。The following example shows how to obtain the system drive and system root variables.

// 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() 
    {
    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
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 interop is used to retrieve the environment variables from the operating system. 如果由于 COM 错误而无法检索环境变量,则将使用用于说明失败原因的 HRESULT 来生成几个可能的异常之一;也就是说,异常依赖于 HRESULT。If the environment variables cannot be retrieved due to a COM error, the HRESULT that explains the cause of the failure is used to generate one of several possible exceptions; that is, the exception depends on the HRESULT. 有关如何处理 HRESULT 的详细信息,请参阅 Marshal.ThrowExceptionForHR 方法的 "备注" 部分。For more information about how the HRESULT is processed, see the Remarks section of the Marshal.ThrowExceptionForHR method.

仅对设置的环境变量进行替换。Replacement only occurs for environment variables that are set. 例如,假设 name 是 "MyENV =% MyENV%"。For example, suppose name is "MyENV = %MyENV%". 如果环境变量 MyENV 设置为42,则此方法将返回 "MyENV = 42"。If the environment variable, MyENV, is set to 42, this method returns "MyENV = 42". 如果未设置 MyENV,则不进行任何更改;此方法返回 "MyENV =% MyENV%"。If MyENV is not set, no change occurs; this method returns "MyENV = %MyENV%".

返回值的大小限制为32K。The size of the return value is limited to 32K.

适用于