Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System
Environment Class
Environment Methods
 ExpandEnvironmentVariables Method
Collapse All/Expand All Collapse All
.NET Framework Class Library
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)
Visual Basic
Public Shared Function ExpandEnvironmentVariables ( _
    name As String _
) As String
C#
public static string ExpandEnvironmentVariables(
    string name
)
Visual C++
public:
static String^ ExpandEnvironmentVariables(
    String^ name
)
F#
static member ExpandEnvironmentVariables : 
        name:string -> string 

Parameters

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

Return Value

Type: System..::.String
A string with each environment variable replaced by its value.
ExceptionCondition
ArgumentNullException

name is nullNothingnullptra 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.

Visual Basic
' Sample for the Environment.ExpandEnvironmentVariables method
Imports System

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 'Main
End Class 'Sample
'
'This example produces the following results:
'
'ExpandEnvironmentVariables:
'  My system drive is C: and my system root is C:\WINNT
'
C#
// 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
*/
Visual C++
// 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
*/

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Only direct expansion, it does not do DOS/CMD style expansion      Cybericom   |   Edit   |   Show History
Unfortunately this will not expand %varName:text=replacementtext% as works in a DOS or CMD command line to replace a part of the text with other text
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker