Пример #1
0
        private static void GetLocallyLoggedOnUsers(string computerName, SyncList.SyncList <DprCurrentUsers> result)
        {
            var usersList = new List <DprCurrentUsers>( );

            using (var regHku = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, string.Empty)) {
                foreach (var currentSid in regHku.GetSubKeyNames( ).Where(IsSid))
                {
                    var cu = new DprCurrentUsers(computerName)
                    {
                        Sid = currentSid
                    };
                    try {
                        if (Win32.WellKnownSids.ContainsKey(currentSid))
                        {
                            cu.Domain   = computerName;                                 // Local account
                            cu.UserName = Win32.WellKnownSids[currentSid];
                        }
                        else
                        {
                            GetUserAccountFromSid(ref cu);
                        }
                        cu.ProfileFolder = RegistryHelpers.GetString(computerName, RegistryHive.LocalMachine, string.Format(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{0}", currentSid), @"ProfileImagePath");
                        cu.LastLogon     = GetUsersLogonTimestamp(cu);
                    } catch (Exception ex) {
                        GlobalLogging.WriteLine(Logging.LogSeverity.Error, @"Exception - {0} - {1}", ex.TargetSite, ex.Message);
                        cu = new DprCurrentUsers(computerName, ConnectionStatuses.Error)
                        {
                            Sid = currentSid
                        };
                    }
                    cu.LogonType = LogonTypes.Local;
                    usersList.Add(cu);
                }
            }
            result.AddRange(usersList);
        }
//      public bool ShouldHide { get { return IsHidden( ); } }
//
//      private bool IsHidden( bool shown = false ) {
//          return !shown && SystemComponent;
//      }

        public static void Generate(string computerName, SyncList <DprComputerSoftware> result)
        {
            Debug.Assert(null != result, @"result SyncList cannot be null");
            Helpers.AssertString(computerName, @"Computer name cannot be empty");
            var softwareList = new List <DprComputerSoftware>();

            try {
                string[] regPaths =
                {
                    @"SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall", @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
                };
                foreach (var currentPath in regPaths)
                {
                    using (var regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computerName).OpenSubKey(currentPath, false)) {
                        if (null == regKey)
                        {
                            continue;
                        }
                        foreach (var currentGuid in regKey.GetSubKeyNames( ).Where(currentValue => currentValue.StartsWith(@"{")).Where(currentGuid => !HasGuid(softwareList, currentGuid)))
                        {
                            using (var curReg = regKey.OpenSubKey(currentGuid, false)) {
                                if (null == curReg || !string.IsNullOrEmpty(RegistryHelpers.GetString(curReg, @"ParentKeyName")))
                                {
                                    continue;
                                }
                                var currentProduct = new DprComputerSoftware(computerName)
                                {
                                    Guid        = currentGuid,
                                    Name        = RegistryHelpers.GetString(curReg, @"DisplayName"),
                                    Publisher   = RegistryHelpers.GetString(curReg, @"Publisher"),
                                    Version     = RegistryHelpers.GetString(curReg, @"DisplayVersion"),
                                    InstallDate = RegistryHelpers.GetDateTime(curReg, @"InstallDate"),
                                    CanRemove   = 0 == RegistryHelpers.GetDword(curReg, @"NoRemove", 0),
                                    //SystemComponent = 1 == RegistryHelpers.GetDword( curReg, @"SystemComponent", 0 )
                                };
                                {
                                    var estSize = RegistryHelpers.GetDword(curReg, @"EstimatedSize");
                                    if (null != estSize)
                                    {
                                        currentProduct.Size = (float)Math.Round((float)estSize / 1024.0, 2, MidpointRounding.AwayFromZero);
                                    }
                                }

                                currentProduct.HelpLink     = RegistryHelpers.GetString(curReg, @"HelpLink");
                                currentProduct.UrlInfoAbout = RegistryHelpers.GetString(curReg, @"UrlInfoAbout");
                                if (currentProduct.Valid( ))
                                {
                                    softwareList.Add(currentProduct);
                                }
                            }
                        }
                    }
                }
            } catch (System.IO.IOException) {
                result.Add(new DprComputerSoftware(computerName, ConnectionStatuses.ConnectionError));
                softwareList.Clear( );
            } catch (UnauthorizedAccessException) {
                result.Add(new DprComputerSoftware(computerName, ConnectionStatuses.AuthorizationError));
                softwareList.Clear( );
            } catch (System.Security.SecurityException) {
                result.Add(new DprComputerSoftware(computerName, ConnectionStatuses.AuthorizationError));
                softwareList.Clear( );
            }
            result.AddRange(softwareList);
            ValidateUniqueness(result);
        }