Could you use setx? I think its an old sysinternals tool. On Oct 23, 2011 12:49 PM, "Greg Keogh" <[email protected]> wrote:
> Several years ago I tried to find a way of setting (creating) DOS > environment variables in code and then using the values in batch files. I > remember I failed because the variables only existed for the duration of the > process. I had a fresh look at this problem today and was thrilled to see a > 3rd argument added to the > SetEnvironmentVariable<http://msdn.microsoft.com/en-us/library/system.environment.setenvironmentvariable.aspx>method. > I wrote a console app with this single line of code: > **** > > ** ** > > Environment.SetEnvironmentVariable("Foo", string.Format("{0:HHmmss}", > DateTime.Now), EnvironmentVariableTarget.User);**** > > ** ** > > In a bat file I then called this app followed by a "set f" command. But > sadly the Foo value is not present. I tried setting target to Machine, but > this requires admin privileges to write to HKLM. Even more sadly it does > work either because it seems to write the reg entry but the new value is not > immediately picked up by the batch file.**** > > ** ** > > Does anyone know of a trick to programmatically set environment variables > for use in a bat file?**** > > ** ** > > Greg**** > > ** ** > > ** ** >
