none
SetEnv does not work RRS feed

  • Question

  • Hello

    I use VS community 2017 15.9.12

    I wish to set an environment variable

    I put this target in a Project.

    $(OracleBaseDir) is not empty (and I have tried with c:\temp, same result)

        <Target Name="SetMyTNSvar" BeforeTargets="ClCompile" >
            <SetEnv  Name="TNS_ADMIN" Prefix="false" Value="$(OracleBaseDir)" />
            <Message Importance="High"
                    Text="TNS_ADMIN envar is $(TNS_ADMIN) " />
        </Target>

    My variable TNS_ADMIN is always empty. (It does not exists in the computer env)

    What do I do wrong ?

    Thank you !

    Tuesday, May 28, 2019 8:52 AM

Answers

  • Wow, glad to know this issue is solved.

    If my reply is helpful, could you please mark it as accepted answer so that other members with similar issue could easily find it.

    Have a nice day!

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JPaul911 Tuesday, June 4, 2019 10:33 AM
    Tuesday, June 4, 2019 10:30 AM

All replies

  • I believe this is because the variable is set for the machine, and not the current process.

    The command placed just after

    <Exec Command="echo TNS_ADMIN envar is %TNS_ADMIN% "/>

    displays the correct value.

    It means that subsequent processes will have the correct value.

    There's a Target optional parameter but I could not see any difference...

    https://docs.microsoft.com/en-us/dotnet/api/system.environmentvariabletarget?view=netframework-4.8

    Tuesday, May 28, 2019 9:52 AM
  • Hi friend,

    Welcome to MSDN forum.

    I've reproduced same issue on my side. And after some tests I found we must specify the Target parameter so that this task could works to set Windows Environment Variable.

    Please:

    1.Run VS as administration to avoid issues about access

    2.Specify the Target parameter to let the task know the value we set belongs to User or Machine:

      <Target Name="SetMyTNSvar" BeforeTargets="ClCompile" >
        <SetEnv  Name="TNS_ADMIN" Prefix="false" Target="User" Value="DoTest" />
        <Message Importance="High"
                 Text="TNS_ADMIN envar is $(TNS_ADMIN) " />
      </Target>
    Then the result would be:

    And 

    Hope it helps. Any feedback would be expected!

    Best Regards

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, May 29, 2019 4:06 AM
  •  Dear Lance

    Yes, the Target parameter changes the behaviour, the variable is set.

    Thank you

    Best regards

    Jean-Paul

    Tuesday, June 4, 2019 10:27 AM
  • Wow, glad to know this issue is solved.

    If my reply is helpful, could you please mark it as accepted answer so that other members with similar issue could easily find it.

    Have a nice day!

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JPaul911 Tuesday, June 4, 2019 10:33 AM
    Tuesday, June 4, 2019 10:30 AM