locked
Set an environment variable RRS feed

  • Question

  • Hi,

    I'm looking for the possibility to set an environment variable temporarily for an application that my VB-application starts.

    My VB-application look like this:

    Dim pExtApplication As New Process

    pExtApplication.StartInfo.FileName = "ExtApplication"

    pExtApplication.StartInfo.Arguments = "--help"

    pExtApplication.StartInfo.UseShellExecute = False

    pExtApplication.StartInfo.RedirectStandardOutput = True

    ' something like Environment.Set("CONFIGURE_SETTING", "NO_CHECKOUT")

    pExtApplication.Start()

    Info = pExtApplication.StandardOutput.ReadLine()

    pExtApplication.WaitForExit()

    If pExtApplication.ExitCode = 0 Then

    bResult = True

    Else

    bResult = False

    End If

    Before I start the external application I want to set the environment varible "CONFIGURE_SETTING". The external application uses this to make special tasks. And it is also clear, that this environment variable varies during runtime and must be set directly before the external application starts.

    So I'm looking for something like "puntenv" or "Environment.Set". For VB I did not find anything.

     

    Thanks

     

    Monday, April 3, 2006 10:26 AM

Answers

  • There are always lots of ways to achieve the desired result.

    To sum up the thread thus far:
    • Use Process.StartInfo.EnvironmentVariables to set environment variables for a process object without using the registry (.NET 1.1 and 2.0). Variables set in this manner are stored in memory and they go away when the process ends.
    • Use Environment.SetEnvironmentVariable to set environment variables using the registry (.NET 2.0). This method can only set variables at the user and machine levels and are copied to memory for new processes when they start. Variables set in this manner persist even after the process ends.
    Clarifications, corrections, or additional comparisons would be appreciated.
    Monday, April 3, 2006 5:08 PM

All replies

  • How about pExtApplication.StartInfo.EnvironmentVariables.Add("CONFIGURE_SETTING", "value") ? You didn't mention whether you had tried that or not.
    Monday, April 3, 2006 11:12 AM
  • How about

    System.Environment.SetEnvironmentVariable

    System.Environment.GetEnvironmentVariable

    Monday, April 3, 2006 3:49 PM
  • Environment.SetEnvironmentVariable applies to the current process only (unless you use the EnvironmentVariableTarget overload; see MSDN). Baerin needs an evironment variable to be set for a new process so this approach probably wouldn't work.
    Monday, April 3, 2006 4:11 PM
  • Well looking at the overload it looks like you can limit the scope of the environmental variable to the current process, current user or local machine.

    In two of the three here - it looks like you'll be able to achieve the desired result.

    Also to note this appears to be Version 2.0 framework functionality.

    Monday, April 3, 2006 4:18 PM
  • There are always lots of ways to achieve the desired result.

    To sum up the thread thus far:
    • Use Process.StartInfo.EnvironmentVariables to set environment variables for a process object without using the registry (.NET 1.1 and 2.0). Variables set in this manner are stored in memory and they go away when the process ends.
    • Use Environment.SetEnvironmentVariable to set environment variables using the registry (.NET 2.0). This method can only set variables at the user and machine levels and are copied to memory for new processes when they start. Variables set in this manner persist even after the process ends.
    Clarifications, corrections, or additional comparisons would be appreciated.
    Monday, April 3, 2006 5:08 PM
  • Hi All,

     

    thanks for your answers.

    The solution to use Process.StartInfo.EnvironmentVariables.Add(key,value) was exact what I looked for.

    When searching in the docoumentation, I did not realize that I can use the Add method for the property EnvironmentVariables.

     

    Bye

    Tuesday, April 4, 2006 7:25 AM