By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

How to set environment variable of another user in windows

P: 6
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
Share this Question
Share on Google+
4 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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

P: 6
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
Expert 2.5K+
P: 3,525
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
Expert 5K+
P: 7,872
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.