472,954 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to set environment variable of another user in windows

I need to create an environment variable for another local user in windows. For example, say suppose there are 2 users in the system (User1 and User2). Currently User1 is logged in the system. Now Is there any way we can create user level environment variable for User2 using C# .Net 2.0.

If we use Environment.SetEnvironmentVariable(envName, envValue, EnvironmentVariableTarget.User) then it will create the environment variable for the currently logged in user i.e. User1.

Even if we try setting the registry key HKEY_CURRENT_USER\Environment still it will create the variable for currently logged in user.
Oct 30 '09 #1
4 16460
tlhintoq
3,525 Expert 2GB
Is this just a value you need for your software to look for?
So long as your program knows where to look to get the value then you're good, right?
How about putting it in HKEY_LOCAL_MACHINE\Environment then?
Oct 30 '09 #2
Thanks for the suggestion. Actually in production site, this environment variable will hold some important information (as per design) hence it should be visible to only this user account. Can't put it in HKEY_LOCAL_MACHINE\Environment.
Is there any way we can achieve this by code.
Oct 30 '09 #3
tlhintoq
3,525 Expert 2GB
So while you are logged in as "UserAlpha" you need to specifically put it in the registry for "UserBravo"?

HKEY_CURRENT_USER *I THINK* is created each time a user logs in, from a stored registry under HKEY_USERS

Use RegEdit and take a look at the HKEY_USERS hive.
You should see some long node names like:
S-1-5-20-2944311053-3597652464-123456789123-1234
That *I THINK* is the stored user registry. So if you can determine which of these is your target user and store there, then *I THINK* that will load up for your user when the user logs in.

Please let us all know if that works.
Oct 30 '09 #4
Plater
7,872 Expert 4TB
You can determine who is who using WMI calls to WMI_UserAccount

Expand|Select|Wrap|Line Numbers
  1. ry
  2.             {
  3.                 ManagementObjectSearcher searcher = 
  4.                     new ManagementObjectSearcher("root\\CIMV2", 
  5.                     "SELECT * FROM Win32_UserAccount"); 
  6.  
  7.                 foreach (ManagementObject queryObj in searcher.Get())
  8.                 {
  9.                     Console.WriteLine("-----------------------------------");
  10.                     Console.WriteLine("Caption: {0}", queryObj["Caption"]);
  11.                     Console.WriteLine("FullName: {0}", queryObj["FullName"]);
  12.                     Console.WriteLine("Name: {0}", queryObj["Name"]);
  13.                     Console.WriteLine("SID: {0}", queryObj["SID"]);
  14.                 }
  15.             }
  16.             catch (ManagementException e)
  17.             {
  18.                 MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
  19.             }
  20.  
The SID should match one of the keys in HKEY_USERS (They will need to have logged into the computer once prior for a key to exist)
Then just pick the subkey "Environment" or "VOLATILE Environment"
Oct 30 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: sebastien.hugues | last post by:
Hi I would like to retrieve the application data directory path of the logged user on windows XP. To achieve this goal i use the environment variable APPDATA. The logged user has this name:...
2
by: could ildg | last post by:
To a environment variable in Windows, can python know if it is a system environment variable or a current-user environment variable? -- ðÐðÄ´ÏÃ÷¾ø¶¥¡¢¸ãЦ֮¼«£¬ÊÇÈËÀàµÄºÃÅóÓÑ¡£...
28
by: Christian | last post by:
Another question from a not even newbie: In Unix you can set an environment variable with the command export PYTHONPATH but I would like to set the variable from at .py script. So my question...
1
by: Bonj | last post by:
Hi My application installs a front-end GUI, which runs code when buttons are clicked. It also installs a command-line utility, that is a console application. They are both installed to the...
4
by: | last post by:
Hi all, I am trying to append a certain string to the PATH environment variable programmatically. I am able to read what is in the variable using the System.Environment method...
6
by: yaron | last post by:
Hi, my application use environment variable, let call it FOO. how can i add the FOO environment variable to my project or my solution, so the line string foo =...
6
by: Fuzzyman | last post by:
Hello all, I would like to set a Windows Environment variable for another (non-child) process. This means that the following *doesn't* work : :: os.environ = value In the ``win32api``...
2
by: Ran Raj | last post by:
In Windows environment, I want to create/update both system and user environment variables. By default, I can access/modify the variables using Start > Control panel > System > Advanced >...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.