473,322 Members | 1,690 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,322 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 16524
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.