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

0개 이상의 환경 변수 이름이 포함된 문자열입니다. 각 환경 변수는 백분율 기호(%)로 묶어야 합니다.

반환

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 interop는 운영 체제에서 환경 변수를 검색하는 데 사용됩니다. COM 오류로 인해 환경 변수를 검색할 수 없는 경우 오류의 원인을 설명하는 HRESULT를 사용하여 몇 가지 가능한 예외 중 하나를 생성합니다. 즉, 예외는 HRESULT에 따라 달라집니다. HRESULT가 처리되는 방법에 대한 자세한 내용은 메서드의 설명 섹션을 Marshal.ThrowExceptionForHR 참조하세요.

대체는 설정된 환경 변수에 대해서만 발생합니다. 예를 들어 "MyENV = %MyENV%"라고 가정 name 합니다. 환경 변수인 MyENV가 42로 설정된 경우 이 메서드는 "MyENV = 42"를 반환합니다. MyENV가 설정되지 않은 경우 변경이 발생하지 않습니다. 이 메서드는 "MyENV = %MyENV%"를 반환합니다.

반환 값의 크기는 32K로 제한됩니다.

적용 대상