473,835 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Environment Variables Go "Poof"

Hi,

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

System.Environm ent.SetEnvironm entVariable("TC S", "C:\Program Files\TCS\")
InstallPath = System.Environm ent.GetEnvironm entVariable("TC S")
MsgBox(InstallP ath)

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.Environm ent.SetEnvironm entVariable are volatile; that is, they don't
exist outside that currently running application.

How can I set it programmaticall y, and have it STAY there?

Regards,
Scott
Mar 6 '08 #1
7 1556
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 programmaticall y. I've
added these three lines to my code:

System.Environm ent.SetEnvironm entVariable("TC S", "C:\Program
Files\TCS\") InstallPath =
System.Environm ent.GetEnvironm entVariable("TC S") MsgBox(InstallP ath)

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.Environm ent.SetEnvironm entVariable are volatile; that is, they
don't exist outside that currently running application.

How can I set it programmaticall y, and have it STAY there?

Regards,
Scott

Mar 6 '08 #2
Tom Shelton <to*********@YO UKNOWTHEDRILLco mcast.netwrote in
news:uX******** ******@TK2MSFTN GP05.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*****@beache xpress.takethis partout.comwrot e:
Tom Shelton <to*********@YO UKNOWTHEDRILLco mcast.netwrote in
news:uX******** ******@TK2MSFTN GP05.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*****@beache xpress.takethis partout.comwrot e in
news:Xn******** *************** **********@207. 46.248.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*****@beache xpress.takethis partout.comwrot e in
news:Xn******** *************** ***********@207 .46.248.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*****@beache xpress.takethis partout.comwrot e:
Scott McNair <sm*****@beache xpress.takethis partout.comwrot e in
news:Xn******** *************** ***********@207 .46.248.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*********@co mcast.netwrote in news:928f4a03-d99d-4d8b-
93************* **@d62g2000hsf. googlegroups.co m:
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
9329
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 mm/yyyy". Means: if the user enters "10/2004 - 10/2005" everything between "2004-10-01" and "2004-10-31" is selected.
7
3422
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 the same test scenario on a production box: no problems. Tried them on my developer box: no problems. In each of those two cases, the PC's were connected to the same test server and *supposedly* had the same production build image on them. ...
15
2433
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. They (my co-workers) say (with vehement ardor ;) ) that the usage of setjmp() emplyoyed in function"C" that was called from function "B" that was called from function "A" that was called form the main(), will cause havoc in the stack. And it makes...
8
3789
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 with all the names of my testfiles and a testdirectory. The tests call the function (which opens and writes to the file) and then opens the file to see if everything is in order. The problem now is that after each testrun I have to copy "fresh"...
5
2903
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 implement this. I've done some VB6 programming and dabbled in VS.Net. Can someone share some pointers in how I could implement the following? Basically, you drag a file to the "smart" folder and, depending on the type of file and settings for that...
0
9803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.