Using TRegistry

From Appmethod Topics
Jump to: navigation, search

Go Up to Working with ini Files and the System Registry

If you are writing a Windows-only application and are comfortable with the structure of the system Registry, you can use System.Win.Registry.TRegistry. Unlike TRegistryIniFile, which uses the same properties and methods of other ini file components, the properties and methods of TRegistry correspond more directly to the structure of the system Registry. You can specify both the root key and subkey using TRegistry, while TRegistryIniFile uses HKEY_CURRENT_USER as the root key.

In addition to methods for opening, closing, saving, moving, copying, and deleting keys, TRegistry lets you specify the access level you want to use.

Note: TRegistry is not available for cross-platform (non Windows) programming.

The following example retrieves a value from a registry entry:

Object Pascal:

 
 function GetRegistryValue(KeyName: string): string;
 var
   Registry: TRegistry;
 begin
   Registry := TRegistry.Create(KEY_READ);
   try
     Registry.RootKey = HKEY_LOCAL_MACHINE;
 
     // False because we do not want to create it if it doesn't exist
     Registry.OpenKey(KeyName, False);
 
     Result := Registry.ReadString('VALUE1');
   finally
     Registry.Free;
   end;
 end;

C++:

#include <Registry.hpp>

AnsiString GetRegistryValue(AnsiString KeyName) {
	AnsiString S;
	TRegistry *Registry = new TRegistry(KEY_READ);
	try {
		Registry->RootKey = HKEY_LOCAL_MACHINE;
		// False because we do not want to create it if it doesn’t exist
		Registry->OpenKey(KeyName, false);
		S = Registry->ReadString("VALUE1");
	}
	__finally {
		delete Registry;
	}
	return S;
}

See Also

Code Examples