Microsoft KB Archive/829145

= System.Environment class does not have a method to set the environment variable for the current process =

Article ID: 829145

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
The System.Environment class has methods to read the environment variables. However, this class has no method to set the environment variables for the current process.



WORKAROUND
To work around this problem, use the interop services to set the environment variables. You can set an environment variable by using the Microsoft Platform Software Development Kit (SDK) SetEnvironmentVariable function.

To set an environment variable by calling the Platform SDK SetEnvironmentVariable function, follow these steps:  Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. On the File menu, point to New, and then click New Project. In the New Project dialog box, click Visual C# Projects.

Note In Visual Studio 2005, Visual C# Projects is be changed to Visual C#. Under Templates, click Console Application, and then click OK. By default, the Class1.cs file is created.  In the code view of the Class1.cs file, specify the using statement to declare the namespaces so that you do not have to qualify the declarations later in the code. Paste the following code in the Class1.cs file: using System; using System.Runtime.InteropServices; using System.Security; using System.Security.Permissions;   Declare the static method and the extern method. Use the DllImport attribute to import the Kernel32.dll file. This declaration indicates that the definition of the function is outside the code. To do this, paste the following code in the Class1.cs file: // Import the Kernel32 dll file. [DllImport(&quot;kernel32.dll&quot;,CharSet=CharSet.Auto, SetLastError=true)]

[return:MarshalAs(UnmanagedType.Bool)]

// The declaration is similar to the SDK function public static extern bool SetEnvironmentVariable(string lpName, string lpValue);   Paste the following code in the Class1.cs file to add a static method in the class that calls the SetEnvironmentVariable method and that sets the environment variable: public static bool SetEnvironmentVariableEx(string environmentVariable, string variableValue) {   try {       // Get the write permission to set the environment variable. EnvironmentPermission environmentPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Write,environmentVariable);

environmentPermission.Demand;

return SetEnvironmentVariable(environmentVariable, variableValue); }   catch( SecurityException e)    { Console.WriteLine(&quot;Exception:&quot; + e.Message); }   return false; } </li>  Paste the following code in the Main method to set an environment variable: // Create a sample environment variable and set its value (for the current process). SampleSetEnvironmentVariable.SetEnvironmentVariableEx(&quot;TESTENV&quot;, &quot;TestValue&quot;); </li>  Paste the following code to display the environment variable value: // Verify that environment variable is set correctly. Console.WriteLine(&quot;The value of TESTENV is: &quot; + Environment.GetEnvironmentVariable(&quot;TESTENV&quot;)); </li></ol>

Complete Code Sample
using System; using System.Runtime.InteropServices; using System.Security; using System.Security.Permissions;

namespace SetEnv {   ///     /// Summary description for Class1. ///    public class SampleSetEnvironmentVariable { // Import the kernel32 dll. [DllImport(&quot;kernel32.dll&quot;,CharSet=CharSet.Auto, SetLastError=true)]

[return:MarshalAs(UnmanagedType.Bool)]

// The declaration is similar to the SDK function public static extern bool SetEnvironmentVariable(string lpName, string lpValue);

public SampleSetEnvironmentVariable {       }

public static bool SetEnvironmentVariableEx(string environmentVariable, string variableValue) {           try {               // Get the write permission to set the environment variable. EnvironmentPermission environmentPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Write,environmentVariable);

environmentPermission.Demand;

return SetEnvironmentVariable(environmentVariable, variableValue); }           catch( SecurityException e)            { Console.WriteLine(&quot;Exception:&quot; + e.Message); }           return false; }   }

class MyClass {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           // Create a sample environment variable and set its value (for the current process). SampleSetEnvironmentVariable.SetEnvironmentVariableEx(&quot;TESTENV&quot;, &quot;TestValue&quot;);

// Verify that environment variable is set correctly. Console.WriteLine(&quot;The value of TESTENV is: &quot; + Environment.GetEnvironmentVariable(&quot;TESTENV&quot;)); }   } }

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

<div class="references_section">