468,554 Members | 1,418 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

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 15004
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

Post your reply

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

Similar topics

8 posts views Thread by sebastien.hugues | last post: by
28 posts views Thread by Christian | last post: by
1 post views Thread by Bonj | last post: by
4 posts views Thread by | last post: by
6 posts views Thread by Fuzzyman | last post: by
4 posts views Thread by Stephen Cattaneo | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.