locked
Environment.SetEnvironmentVariable does not set any value RRS feed

  • Question

  • User-1813520879 posted

    I'm encountering this issue with only

    • Windows Server 2016 (Win2016) and
    • VisualStudio-redistributable 2015 (VS-redist-2015 = msvcp14)

    c# Environment.SetEnvironmentVariable(key,val, EnvironmentVariableTarget.Process) does not seem to set values that can be retrieved on the native-C app using getenv()

    native-C app getenv() can read the values OK using Win2016 + VS-redist-2010

    c# -> native-C app communication is through PInvoke (using SWIG), 

    and c# and native-C app runs in worker process in IIS.

    This works fine in Windows Server 2012 and 2008 using VS-redist-2015

    Any thought / suggestion will be appreciated.

    ps. not sure which forum this question should be posted, so i posted it  here.

    Wednesday, March 21, 2018 6:14 AM

All replies

  • User283571144 posted

    Hi Porto,

    According to your description, I guess the reason why you couldn't get the environment value is the  native-C is not use the same process which the C# runs.

    I suggest you could try to use below codes:

     Environment.SetEnvironmentVariable(key,val, EnvironmentVariableTarget.Machine)

    If you set this , the environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment key in the Windows operating system registry.

    Details, you could refer to below article.

    https://msdn.microsoft.com/en-us/library/system.environmentvariabletarget%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 

    Best Regards,

    Brando

    Thursday, March 22, 2018 9:05 AM
  • User-1813520879 posted

    Thanks, it works, although not ideal.

    As the application runs in IIS, the application pool needs elevated permission to execute 

     Environment.SetEnvironmentVariable(key,val, EnvironmentVariableTarget.Machine)
    

    Might be easier if the environment variables is added under SYSTEM variable once.

    Monday, February 11, 2019 7:26 AM