473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Environment Variables Go "Poof"

Hi,

I'm trying to set an environment variable programmatically. I've added
these three lines to my code:

System.Environment.SetEnvironmentVariable("TCS", "C:\Program Files\TCS\")
InstallPath = System.Environment.GetEnvironmentVariable("TCS")
MsgBox(InstallPath)

And, as expected, the MsgBox contains "C:\Program Files\TCS\".

However the variable seems to exist only for that instance of the app. For
example, if I open a cmd window while the app is running and type "echo %
TCS%", it just returns "%TCS%". If I exit the app, remark out the first
line, and then re-run the app, the MsgBox returns an empty box.

I'm guessing that any environment variables you set using
System.Environment.SetEnvironmentVariable are volatile; that is, they don't
exist outside that currently running application.

How can I set it programmatically, and have it STAY there?

Regards,
Scott
Mar 6 '08 #1
7 1539
Take a look at this, which might help you. It may come down to updating
the registry.

http://ghouston.blogspot.com/2005/08...vironment.html
Hi,

I'm trying to set an environment variable programmatically. I've
added these three lines to my code:

System.Environment.SetEnvironmentVariable("TCS", "C:\Program
Files\TCS\") InstallPath =
System.Environment.GetEnvironmentVariable("TCS") MsgBox(InstallPath)

And, as expected, the MsgBox contains "C:\Program Files\TCS\".

However the variable seems to exist only for that instance of the app.
For example, if I open a cmd window while the app is running and type
"echo % TCS%", it just returns "%TCS%". If I exit the app, remark out
the first line, and then re-run the app, the MsgBox returns an empty
box.

I'm guessing that any environment variables you set using
System.Environment.SetEnvironmentVariable are volatile; that is, they
don't exist outside that currently running application.

How can I set it programmatically, and have it STAY there?

Regards,
Scott

Mar 6 '08 #2
Tom Shelton <to*********@YOUKNOWTHEDRILLcomcast.netwrote in
news:uX**************@TK2MSFTNGP05.phx.gbl:
If you have any issues let me know and I'll actually look up
SendMessage and test some code :)
Thanks, Tom.

I entered your code, and it seems to hang when I use SendMessage.
Mar 6 '08 #3
On 2008-03-06, Scott McNair <sm*****@beachexpress.takethispartout.comwrote:
Tom Shelton <to*********@YOUKNOWTHEDRILLcomcast.netwrote in
news:uX**************@TK2MSFTNGP05.phx.gbl:
>If you have any issues let me know and I'll actually look up
SendMessage and test some code :)

Thanks, Tom.

I entered your code, and it seems to hang when I use SendMessage.
I tested it here just now - and it is working. The call can take
a few moments, because it has to notify all the top level windows on the
system - and SendMessage will wait for all to return...

Are us sure it is hanging permanetly?
--
Tom Shelton
Mar 6 '08 #4
Scott McNair <sm*****@beachexpress.takethispartout.comwrote in
news:Xn*********************************@207.46.24 8.16:
Just to make sure, I started the app and let it sit there, to see how
long it would take (if ever) to execute. So far it's been sitting for
about ten minutes.
As a followup:

Rather than run the app in debug mode has I had been doing, I compiled it
as a release, and then ran that app. It ran within a second, but it
returned nothing in the MsgBox that supposedly is polling the environment
variable.

I then ran it again, and it returned the proper string in the MsgBox. I
then went into the app and disabled the code that sets the variable,
effectively having the MsgBox be my first line; it still reported the
variable.

So I decided to run it again. I deleted the value from the registry and
reran the application. This time it reported the value properly on the
first execution.

I continued my experiment. I deleted the key and I remarked everything
out. I recompiled the app, and ran it. It still reported the key. "No
problem," I thought, "it just hasn't sent the environment refresh."

So this time I left the registry write remarked, but I unremarked the
SendMessage line. I ran it again, and it STILL reported C:\Program Files
\TCS\ as the value of the variable, even though it shouldn't be there.

The only explanation I can think of is that the SendMessage command only
updates and inserts, but it doesn't actually clear removed items from the
current environment.

Does that sound about right?
Mar 6 '08 #5
Scott McNair <sm*****@beachexpress.takethispartout.comwrote in
news:Xn**********************************@207.46.2 48.16:
The only explanation I can think of is that the SendMessage command
only updates and inserts, but it doesn't actually clear removed items
from the current environment.
Yet more followup:

If I run the app and declare the variable, it does not see the variable
within that instance of the app. I set a timer for five seconds to enable
immediately after SendMessage, and then MsgBoxed the variable; it showed a
blank.

If I then uncomment the code that creates the variable, and recompile the
app, the variable is there when I run it.

I'm guessing that's not typical execution, but I've run it about three or
four times, with the same result each time.
Mar 6 '08 #6
On 2008-03-06, Scott McNair <sm*****@beachexpress.takethispartout.comwrote:
Scott McNair <sm*****@beachexpress.takethispartout.comwrote in
news:Xn**********************************@207.46.2 48.16:
>The only explanation I can think of is that the SendMessage command
only updates and inserts, but it doesn't actually clear removed items
from the current environment.

Yet more followup:

If I run the app and declare the variable, it does not see the variable
within that instance of the app. I set a timer for five seconds to enable
immediately after SendMessage, and then MsgBoxed the variable; it showed a
blank.

If I then uncomment the code that creates the variable, and recompile the
app, the variable is there when I run it.

I'm guessing that's not typical execution, but I've run it about three or
four times, with the same result each time.
Scott... I can play with this a little more - but, I want to ask before
we go much further (probably should have done this first!), but what is
the ultimate goal here? We maybe going down the wrong road :)

--
Tom Shelton
Mar 6 '08 #7
Tom Shelton <to*********@comcast.netwrote in news:928f4a03-d99d-4d8b-
93***************@d62g2000hsf.googlegroups.com:
Kind of rough, but it seems to work ok :) At least on XP - I suspect
you might have permission issues on Vista ;)
Thank you :)
Mar 7 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Bernd Hohmann | last post by:
Hi there, we're currently convert an old ISAM application to Java and DB2. Among the load of problems we found the reports which have user selections like "select all data between mm/yyyy and...
7
by: (Pete Cresswell) | last post by:
We were testing a version of our app that's been running for months with no problems and it started throwing "Object no longer exists" messages on two machines in the test environment. We tried...
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers....
8
by: killkolor | last post by:
hi! I have a unittest framework that tests a single function that in turn works with files (takes input and outputs in the same file, no return values). In the unittest class I assign a member...
5
by: Noozer | last post by:
I'm looking for a "smart folder" program to run on my Windows XP machine. I'm not having any luck finding it and think the logic behind the program is pretty simple, but I'm not sure how I'd...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.