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

0 個以上の環境変数の名前を格納している文字列。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.

例外

namenull です。name 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 を使用して、考えられるいくつかの例外の1つを生成します。つまり、例外は 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.

セキュリティ

EnvironmentPermission
name の環境変数にアクセスする権限。for the ability to access the environment variables in name. 関連付けられた列挙型: ReadAssociated enumeration: Read

適用対象