How to set environment variable of another user in windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vkbishnoi
    New Member
    • Oct 2009
    • 6

    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.Set EnvironmentVari able(envName, envValue, EnvironmentVari ableTarget.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_US ER\Environment still it will create the variable for currently logged in user.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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_MACH INE\Environment then?

    Comment

    • vkbishnoi
      New Member
      • Oct 2009
      • 6

      #3
      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_MACH INE\Environment .
      Is there any way we can achieve this by code.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        So while you are logged in as "UserAlpha" you need to specifically put it in the registry for "UserBravo" ?

        HKEY_CURRENT_US ER *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.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You can determine who is who using WMI calls to WMI_UserAccount

          [code=c#]
          ry
          {
          ManagementObjec tSearcher searcher =
          new ManagementObjec tSearcher("root \\CIMV2",
          "SELECT * FROM Win32_UserAccou nt");

          foreach (ManagementObje ct queryObj in searcher.Get())
          {
          Console.WriteLi ne("-----------------------------------");
          Console.WriteLi ne("Caption: {0}", queryObj["Caption"]);
          Console.WriteLi ne("FullName: {0}", queryObj["FullName"]);
          Console.WriteLi ne("Name: {0}", queryObj["Name"]);
          Console.WriteLi ne("SID: {0}", queryObj["SID"]);
          }
          }
          catch (ManagementExce ption e)
          {
          MessageBox.Show ("An error occurred while querying for WMI data: " + e.Message);
          }
          [/code]
          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 "Environmen t" or "VOLATILE Environment"

          Comment

          Working...