none
Percorrer Regedit RRS feed

Todas as Respostas

  • Eu fiz essa classe para ler todos os programas instalados no PC; ela faz isso varrendo o registro, mas não na "CurrentUser"; o que você pode alterar facilmente para fazer.

    using Sys = global::System;
    using SysCllG = global::System.Collections.Generic;
    using Win32 = global::Microsoft.Win32;
    
    [Sys.Serializable] public struct Software : Sys.IComparable<Software>, Sys.IEquatable<Software>
    {
        private string data_DisplayName;
        private string data_Version;
        public string DisplayName { get { return this.data_DisplayName; } }
        public string Version { get { return this.data_Version; } }
    
        private static SysCllG.List<Software> InternalGetInstalledPrograms(Win32.RegistryKey HklmPath, Win32.RegistryKey HkuPath)
        {
            SysCllG.List<Software> ProgramList = new SysCllG.List<Software>();
            Win32.RegistryKey ClassesKey = HklmPath.OpenSubKey("Software\\Classes\\Installer\\Products");
            Win32.RegistryKey Wow64UninstallKey = HklmPath.OpenSubKey("Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
            ProgramList = Software.GetUninstallKeyPrograms(Wow64UninstallKey, ClassesKey, ProgramList);
            Win32.RegistryKey StdUninstallKey = HklmPath.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
            ProgramList = Software.GetUninstallKeyPrograms(StdUninstallKey, ClassesKey, ProgramList);
            Win32.RegistryKey CuKey = null;
            foreach (string UserSid in HkuPath.GetSubKeyNames())
            {
                CuKey = HkuPath.OpenSubKey(UserSid + "\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
                ProgramList = Software.GetUninstallKeyPrograms(CuKey, ClassesKey, ProgramList);
                CuKey = HkuPath.OpenSubKey(UserSid + "\\Software\\Microsoft\\Installer\\Products");
                ProgramList = Software.GetUserInstallerKeyPrograms(CuKey, HklmPath, ProgramList);
            }
            try
            {
                HklmPath.Close();
                HkuPath.Close();
            } catch { /* NOTHING */ }
            ProgramList.Sort();
            return ProgramList;
        }
    
        private static bool IsProgramInList(string ProgramName, SysCllG.List<Software> ListToCheck) { return ListToCheck.Contains(new Software(ProgramName)); }
    
        private static SysCllG.List<Software> GetUserInstallerKeyPrograms(Win32.RegistryKey CuInstallerKey, Win32.RegistryKey HklmRootKey, SysCllG.List<Software> ExistingProgramList)
        {
            if ((CuInstallerKey != null))
            {
                Win32.RegistryKey ProductsKey = null;
                Win32.RegistryKey UserDataProgramKey = null;
                foreach (string CuProductGuid in CuInstallerKey.GetSubKeyNames())
                {
                    bool ProductFound = false;
                    foreach (string UserDataKeyName in HklmRootKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData").GetSubKeyNames())
                    {
                        if (!(UserDataKeyName == "S-1-5-18"))
                        {
                            ProductsKey = HklmRootKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\" + UserDataKeyName + "\\Products");
                            if ((ProductsKey != null))
                            {
                                string[] LmProductGuids = ProductsKey.GetSubKeyNames();
                                foreach (string LmProductGuid in LmProductGuids)
                                {
                                    if (LmProductGuid == CuProductGuid)
                                    {
                                        UserDataProgramKey = HklmRootKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\" + UserDataKeyName + "\\Products\\" + LmProductGuid + "\\InstallProperties");
                                        if (!(Sys.Convert.ToInt32(UserDataProgramKey.GetValue("SystemComponent", 0)) == 1))
                                        {
                                            string Name = CuInstallerKey.OpenSubKey(CuProductGuid).GetValue("ProductName", string.Empty).ToString();
                                            string ProgVersion = string.Empty;
                                            try { ProgVersion = UserDataProgramKey.GetValue("DisplayVersion", string.Empty).ToString(); }
                                            catch { /* NOTHING */ }
                                            if (!(Name == string.Empty) && !Software.IsProgramInList(Name, ExistingProgramList))
                                            {
                                                ExistingProgramList.Add(new Software(Name));
                                                ProductFound = true;
                                            }
                                        }
                                        break;
                                    }
                                }
                                if (ProductFound) { break; } else { try { ProductsKey.Close(); } catch { /* NOTHING */ } }
                            }
                        }
                    }
                }
            }
            return ExistingProgramList;
        }
    
        private static SysCllG.List<Software> GetUninstallKeyPrograms(Win32.RegistryKey UninstallKey, Win32.RegistryKey ClassesKey, SysCllG.List<Software> ExistingProgramList)
        {
            if ((UninstallKey != null))
            {
                foreach (string SubKeyName in UninstallKey.GetSubKeyNames())
                {
                    try
                    {
                        Win32.RegistryKey CurrentSubKey = UninstallKey.OpenSubKey(SubKeyName);
                        int IsSystemComponent = 0;
                        try { IsSystemComponent = Sys.Convert.ToInt32(CurrentSubKey.GetValue("SystemComponent", 0)); } catch { /* NOTHING */ }
                        if (!(IsSystemComponent == 1))
                        {
                            if (!(Sys.Convert.ToInt32(CurrentSubKey.GetValue("WindowsInstaller", 0)) == 1))
                            {
                                Sys.Text.RegularExpressions.Regex WindowsUpdateRegEx = new Sys.Text.RegularExpressions.Regex("KB[0-9]{6}$");
                                string ProgramReleaseType = CurrentSubKey.GetValue("ReleaseType", string.Empty).ToString();
                                string ProgVersion = string.Empty;
                                try { ProgVersion = CurrentSubKey.GetValue("DisplayVersion", string.Empty).ToString(); }
                                catch { /* NOTHING */ }
                                if (!(WindowsUpdateRegEx.Match(SubKeyName).Success == true || !(CurrentSubKey.GetValue("ParentKeyName", string.Empty).ToString() == string.Empty) || ProgramReleaseType == "Security Update" || ProgramReleaseType == "Update Rollup" || ProgramReleaseType == "Hotfix"))
                                {
                                    bool UninstallStringExists = false;
                                    foreach (string valuename in CurrentSubKey.GetValueNames()) { if (string.Equals("UninstallString", valuename, Sys.StringComparison.CurrentCultureIgnoreCase)) { UninstallStringExists = true; break; } }
                                    if (UninstallStringExists)
                                    {
                                        string Name = CurrentSubKey.GetValue("DisplayName", string.Empty).ToString();
                                        if (!(Name == string.Empty) && !Software.IsProgramInList(Name, ExistingProgramList)) { ExistingProgramList.Add(new Software(Name, ProgVersion)); }
                                    }
                                }
                            }
                            else
                            {
                                string ProgVersion = string.Empty;
                                string Name = string.Empty;
                                try
                                {
                                    string MsiKeyName = Software.GetInstallerKeyNameFromGuid(SubKeyName);
                                    Win32.RegistryKey CrGuidKey = ClassesKey.OpenSubKey(MsiKeyName);
                                    if ((CrGuidKey != null)) { Name = CrGuidKey.GetValue("ProductName", string.Empty).ToString(); }
                                } catch { /* NOTHING */ }
                                try { ProgVersion = CurrentSubKey.GetValue("DisplayVersion", string.Empty).ToString(); } catch { /* NOTHING */ }
                                if (!(Name == string.Empty) && !Software.IsProgramInList(Name, ExistingProgramList)) { ExistingProgramList.Add(new Software(Name, ProgVersion)); }
                            }
                        }
                    } catch { /* NOTHING */}
                }
                try { UninstallKey.Close(); } catch { /* NOTHING */ }
            }
            return ExistingProgramList;
        }
    
        private static string GetInstallerKeyNameFromGuid(string GuidName)
        {
            string[] MsiNameParts = GuidName.Replace("{", string.Empty).Replace("}", string.Empty).Split('-');
            Sys.Text.StringBuilder MsiName = new Sys.Text.StringBuilder();
            for (int i = 0; i <= 2; i++) { MsiName.Append(Software.ReverseString(MsiNameParts[i])); }
            for (int j = 3; j <= 4; j++)
            {
                for (int i = 0; i <= MsiNameParts[j].Length - 1; i++)
                {
                    MsiName.Append(MsiNameParts[j][i + 1]);
                    MsiName.Append(MsiNameParts[j][i]);
                    i += 1;
                }
            }
            return MsiName.ToString();
        }
    
        private static string ReverseString(string input)
        {
            char[] Chars = input.ToCharArray();
            Sys.Array.Reverse(Chars);
            return new string(Chars);
        }
    
        public override string ToString() { return this.data_DisplayName; }
        public static SysCllG.List<Software> GetInstalledPrograms() { return Software.InternalGetInstalledPrograms(Win32.Registry.LocalMachine, Win32.Registry.Users); }
        public int CompareTo(Software other) { return string.Compare(this.data_DisplayName, other.data_DisplayName); }
        public bool Equals(Software other) { if (this.data_DisplayName == other.data_DisplayName && this.data_Version == other.data_Version) { return true; } else { return false; } }
        public Software(string ProgramDisplayName) : this() { this.data_DisplayName = ProgramDisplayName; }
        public Software(string ProgramDisplayName, string ProgramVersion) : this(ProgramDisplayName) { this.data_Version = ProgramVersion; }
    }

    E é isso

    terça-feira, 18 de setembro de 2018 22:01
  • Obrigado, por tentar me ajudar, mas este codigo está muito embaralhado, não consigo ler nd nesse cod, alguém poderia me ajudar com algumas documentação?
    quarta-feira, 19 de setembro de 2018 06:16
  • Alguém?
    terça-feira, 25 de setembro de 2018 01:38