Siemka, własnie koduje sobie w Delphi.
Robiłem funkcję która sprawdzi w rejestrze jakie mamy zainstalowane programy, a następnie zapisze do pliku (jak nazwiemy)
Być może komuś się przyda, a jak chce niech modyfikuje na swoje potrzeby.
Kodowane pod/w Embarcadero® Delphi 12 Version 29.0.51961.7529
Do uses należy dodać: System.Generics.Collections, Registry;
oto i ona :
// CODE BY FPERSON in Embarcadero® Delphi 12
type
TLogLanguage = (tlEnglish, tlPolish);
var
EnglishDictionary: TDictionary<string, string>;
PolishDictionary: TDictionary<string, string>;
// Procedure for initializing dictionaries with translations
procedure InitializeDictionaries;
begin
if EnglishDictionary = nil then begin
EnglishDictionary := TDictionary<string, string>.Create;
EnglishDictionary.Add('InstallAppList', 'Installed Programs List:');
EnglishDictionary.Add('Name', 'Name: ');
EnglishDictionary.Add('Version', 'Version: ');
EnglishDictionary.Add('Publisher', 'Publishern: ');
EnglishDictionary.Add('Location', 'Location: ');
EnglishDictionary.Add('ErrAccessKey', 'Error accessing key: ');
end;
if PolishDictionary = nil then begin
PolishDictionary := TDictionary<string, string>.Create;
PolishDictionary.Add('InstallAppList', 'Lista Zainstalowanych Programów:');
PolishDictionary.Add('Name', 'Nazwa: ');
PolishDictionary.Add('Version', 'Wersja: ');
PolishDictionary.Add('Publisher', 'Wydawca: ');
PolishDictionary.Add('Location', 'Lokalizacja: ');
PolishDictionary.Add('ErrAccessKey', 'Błąd przy dostępie do klucza: ');
end;
end;
// free
procedure FinalizeDictionaries;
begin
FreeAndNil(EnglishDictionary);
FreeAndNil(PolishDictionary);
end;
// a function that retrieves the translated word based on the key and language
function GetLocalizedString(Key: string; Language: TLogLanguage): string;
begin
case Language of
tlEnglish:
// English
if EnglishDictionary.TryGetValue(Key, Result) then
Exit;
tlPolish:
// Polish
if PolishDictionary.TryGetValue(Key, Result) then
Exit;
end;
// If not translation found, we return just the key as the default value
Result := Key;
end;
procedure ListInstalledPrograms(const LogFileName: string; Language: TLogLanguage = tlEnglish);
var
Registry: TRegistry;
KeyNames: TStringList;
LogFile: TextFile;
ProgramName, InstallLocation, DisplayVersion, Publisher: string;
procedure ProcessKeys(const RootKey: HKEY; const Path: string);
begin
Registry.RootKey := RootKey;
if Registry.OpenKeyReadOnly(Path) then
begin
Registry.GetKeyNames(KeyNames);
Registry.CloseKey; // Close after fetching key names
for var i := 0 to KeyNames.Count - 1 do
begin
if Registry.OpenKeyReadOnly(Path + '\' + KeyNames[i]) then
begin
try
ProgramName := Registry.ReadString('DisplayName');
InstallLocation := Registry.ReadString('InstallLocation');
DisplayVersion := Registry.ReadString('DisplayVersion');
Publisher := Registry.ReadString('Publisher');
if ProgramName <> '' then
begin
WriteLn(LogFile, GetLocalizedString('Name', Language), ProgramName);
if DisplayVersion <> '' then
WriteLn(LogFile, GetLocalizedString('Version', Language), DisplayVersion);
if Publisher <> '' then
WriteLn(LogFile, GetLocalizedString('Publisher', Language), Publisher);
if InstallLocation <> '' then
WriteLn(LogFile, GetLocalizedString('Location', Language), InstallLocation);
WriteLn(LogFile, 'GUID: ', KeyNames[i]);
WriteLn(LogFile, '--------------------------------');
end;
except
on E: Exception do
begin
WriteLn(LogFile, GetLocalizedString('ErrAccessKey', Language), KeyNames[i], ' - ', E.Message);
WriteLn(LogFile, '--------------------------------');
end;
end;
Registry.CloseKey; // Close after processing the key
end;
end;
end;
end;
begin
Registry := TRegistry.Create(KEY_READ);
KeyNames := TStringList.Create;
InitializeDictionaries();
try
AssignFile(LogFile, LogFileName);
Rewrite(LogFile);
WriteLn(LogFile, GetLocalizedString('InstallAppList', Language));
WriteLn(LogFile, '--------------------------------');
// Process keys from HKEY_LOCAL_MACHINE
ProcessKeys(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall');
// Process keys from HKEY_CURRENT_USER
ProcessKeys(HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall');
CloseFile(LogFile);
finally
KeyNames.Free;
Registry.Free;
FinalizeDictionaries;
end;
end;
Funkcje wywołujemy:
// plik InstalledPrograms.log bedzie zapisany do katalogu gdzie mamy skompilowany EXE
ListInstalledPrograms('InstalledPrograms.log'); // ENGLISH (domyslny)
ListInstalledPrograms('InstalledPrograms.log', tlPolish); // POLSKI
// plik InstalledPrograms.log bedzie zapisany do C:\InstalledPrograms.log
ListInstalledPrograms('C:\InstalledPrograms.log'); // ENGLISH (domyslny)
ListInstalledPrograms('C:\InstalledPrograms.log', tlPolish); // POLSKI
Jeśli to się przyda, super, cieszę się, że mogłem pomóc! Jeśli nie, no cóż, przynajmniej nikt nie stracił złotówki!
Remember, any fool can write code that a computer can understand. Good programmers write code that humans can understand.