Export (0) Print
Expand All

Environment.ExpandEnvironmentVariables Method

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.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public:
static String^ ExpandEnvironmentVariables (
	String^ name
)
public static String ExpandEnvironmentVariables (
	String name
)
public static function ExpandEnvironmentVariables (
	name : String
) : String
Not applicable.

Parameters

name

A string containing the names of zero or more environment variables. Each environment variable is quoted with the percent sign character (%).

Return Value

A string with each environment variable replaced by its value.

Exception typeCondition

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

COM interop is used to retrieve the environment variables from the operating system. 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. 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. For example, suppose name is "MyENV = %MyENV%". If the environment variable, MyENV, is set to 42, this method returns "MyENV = 42". If MyENV is not set, no change occurs; this method returns "MyENV = %MyENV%".

The size of the return value is limited to 32K on Windows NT 4.0 and earlier, and Windows 2000 and later. There is no size restriction on Windows 98 and Windows Me.

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
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        String str;
        String nl = Environment.get_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);
    } //main
} //Sample

/*
This example produces the following results:

ExpandEnvironmentVariables:
  My system drive is C: and my system root is C:\WINDOWS
*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft