Connecting Tech Pros Worldwide Forums | Help | Site Map

How to set environment variable of another user in windows

Newbie
 
Join Date: Oct 2009
Location: Pune, India
Posts: 6
#1: 4 Weeks Ago
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.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,784
#2: 4 Weeks Ago

re: How to set environment variable of another user in windows


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?
Newbie
 
Join Date: Oct 2009
Location: Pune, India
Posts: 6
#3: 4 Weeks Ago

re: How to set environment variable of another user in windows


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.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,784
#4: 4 Weeks Ago

re: How to set environment variable of another user in windows


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.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,162
#5: 4 Weeks Ago

re: How to set environment variable of another user in windows


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"
Reply