Urgent - Set Environment Variables

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

I know that i can use the system.Environment namespace to
get environment variables, but how do i go about creating
a new environment variable and setting it's value in .net?


thanks eric.
 
Hi Eric,

If you need to set these in the context of a new Process you are launching,
check out the ProcessStartInfo.EnvironmentVariables collection. It is read
only but you can get a reference to it and modify the reference directly:

ProcessStartInfo psi = new ProcessStartInfo();
...
string name = "some_env_var";
string val = "c:\adirectory";
psi.EnvironmentVariables[name] = val;

Then you can launch your Process with this psi object.

If this context is not useful to you, you could always import the Win32 API
routine to use it:

[DllImport("kernel32.dll")]
public static extern uint SetEnvironmentVariable(
string lpName,
string lpValue );

Then you can call this method from your managed code (you'll need to
declare using System.Runtime.InteropServices).

Hope this helps, let me know if I can provide any more info.

Jeff.

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
I should have provided a little more insight into what i
am trying to do. I have a console (vb.net) window that
accepts input, i need to make this input avaliable to the
parent console (windows) window. I figured that
environment variables would do the trick, however i find
that when i edit the registry the machine needs a reboot.
Does this make sense? Any ideas?

thanks eric.

-----Original Message-----
Hi Eric,

If you need to set these in the context of a new Process you are launching,
check out the ProcessStartInfo.EnvironmentVariables collection. It is read
only but you can get a reference to it and modify the reference directly:

ProcessStartInfo psi = new ProcessStartInfo();
...
string name = "some_env_var";
string val = "c:\adirectory";
psi.EnvironmentVariables[name] = val;

Then you can launch your Process with this psi object.

If this context is not useful to you, you could always import the Win32 API
routine to use it:

[DllImport("kernel32.dll")]
public static extern uint SetEnvironmentVariable(
lpName,
string lpValue );

Then you can call this method from your managed code (you'll need to
declare using System.Runtime.InteropServices).

Hope this helps, let me know if I can provide any more info.

Jeff.

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

.
 
Hi, Eric

I wouldn't recommend using environment variables for this purpose. Since
you appear to have control of both the client and the parent applications,
the simplest approach would be a shared database. If you want less
overhead or better reaction speed, I would look at remoting. How you
implement this depends on how your parent app uses the information and how
much information there is. Are the actions of the parent driven by the
console, or does it just collect the data on its own schedule?

Please provide more information and I can do a better job of helping you
target an approach.

Thank you for choosing the MSDN Managed Newsgroups,

John Eikanger
Microsoft Developer Support

This posting is provided “AS IS” with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: <[email protected]>
| Sender: <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Urgent - Set Environment Variables
| Date: Wed, 29 Oct 2003 13:42:21 -0800
| Lines: 66
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| I should have provided a little more insight into what i
| am trying to do. I have a console (vb.net) window that
| accepts input, i need to make this input avaliable to the
| parent console (windows) window. I figured that
| environment variables would do the trick, however i find
| that when i edit the registry the machine needs a reboot.
| Does this make sense? Any ideas?
|
| thanks eric.
 
Back
Top