25 sep 2012

Get logged on users

ever tryed to resolve the logged onusers with wmi?
I think it is just the wrong way.


public static List< string> GetLoggedOnUsers(string Computername)
        {
            List<string > Users = new List<string >();
            try
            {
                // Open HKEY_USERS
                // on a remote computer.
              
                RegistryKey environmentKey = RegistryKey.OpenRemoteBaseKey(RegistryHive .Users, Computername);

                foreach (string subKeyName in environmentKey.GetSubKeyNames())
                {

                    RegistryKey rkeyuser = environmentKey.OpenSubKey(subKeyName);
                    RegistryKey rkeyVendor = rkeyuser.OpenSubKey("Volatile Environment");
                    if (rkeyVendor != null )
                    {
                        String[] ValueNames = rkeyVendor.GetValueNames();
                        foreach (string name in ValueNames)
                        {
                            if(name=="USERNAME" )
                                Users.Add(rkeyVendor.GetValue(name).ToString());
                        }
                    }
           
               
                }

                // Close the registry key.
                environmentKey.Close();
            }
            catch
            {
            }
            return Users;
        }

Geen opmerkingen:

Een reactie posten