Problem with GetEnvironmentVariable

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

the GetEnvironmentVariable method returns "Nothing" when used against a
environment variable, created by me.
I've tried using SET from the console, or "Environment Variables" in my
computer properties.
Tested on 2 PCs.

Whats the problem?
 
Also I can read some of the current system variables like "windir".

The example in MSDN also don't show my variable, but I can sse it using
SET from the command prompt

Example source:
' Sample for the Environment.GetEnvironmentVariables method
Imports System
Imports System.Collections

Class Sample
Public Shared Sub Main()
Console.WriteLine()
Console.WriteLine("GetEnvironmentVariables: ")
Dim environmentVariables As IDictionary =
Environment.GetEnvironmentVariables()
Dim de As DictionaryEntry
For Each de In environmentVariables
Console.WriteLine(" {0} = {1}", de.Key, de.Value)
Next de
End Sub 'Main
End Class 'Sample
 
Try restarting the PC after you add your vars into the properties area. I
don't think those are dynamic. They get read in when you boot up and I don't
think they get refreshed without a restart...
 
Ray Cassick said:
Try restarting the PC after you add your vars into the properties area. I
don't think those are dynamic. They get read in when you boot up and I
don't
think they get refreshed without a restart...

It's possible to define environment variables in a batch file which will be
valid until the batch file is processed. I often follow this approach to
start tools which need certain environment variables without the need of
changing the whole system's environment variables.
 
I can see them, without doint restart using the SET command from the
console.
Also can assign new values, works just fine.
Except in my app.
 
Yes, but can you create them in one session (batch file) and see then in
another?

I just opened 2 command windows.. in one I typed 'set tester=me'. Ten in
that same window I typed set and was able to view it. In the second command
window I typed set it was not present. Seems to prove that env vars defined
in a batch files do not cross process boundaries.

Now, I created an env var named tester in my system properties (as a system
env var) and set its value. Once I hit apply I could open a command window
and type set and see it there..
 
Ray Cassick said:
Yes, but can you create them in one session (batch file) and see then in
another?

No, but you should be able to see them in an application that is started
from within the batch file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top