How can I read, set, or delete Windows system registry data?
Anyone who has worked with the Windows platform has probably encountered the system registry at one time or another. The system registry is a database of sorts, organized in a tree like fashion. The system registry contains data on just about everything in the system, including hardware, software, and users. It is important to note that because the system registry is Windows platform specific, ColdFusion only supports the <cfregistry> tag on the Windows platform.
The registry hierarchy tree is organized into two basic units: keys and values. Keys make up the branches in the tree, and values are the actual data (in the form of name value pairs). The <cfregistry> tag allows you to read, set (insert or update), and delete registry data.
<cfregistry> gives you two methods for reading (retrieving) keys and values from the registry. The first method will allow you to retrieve all of the key and value information for an entire branch. This is accomplished by setting the action attribute to getAll. The second method will only get the information for the specific entry you are targeting. This is accomplished by setting the action attribute to get.
The following example will look up information about all of the ODBC connections (the HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI key) setup on the ColdFusion server machine:
<cfregistry action="getAll"
branch="HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI"
name="GetODBC"
type="any"
sort="entry asc">
<table border="1">
<tr>
<td>Entry</td><td>Type</td><td>Value</td>
</tr>
<cfoutoput query="getODBC">
<tr>
<td>#Entry#</td><td>#Type#</td><td>#Value#</td>
</tr>
</cfoutput>
</table>
Setting registry keys and values is accomplished by setting the action attribute to set. Note that the type attribute can be set to key, dWord, or string (default) depending on what type of registry data you need to set. The following code shows how to use <cfregistry> to set a registry value:
<cfregistry action="set"
branch="HKEY_LOCAL_MACHINE\Software\Macromedia\ColdFusion\CurrentVersion"
type="key"
entry="test">
Deleting registry key or values is accomplished by setting the action attribute to delete. The following code shows how to use <cfregistry> to delete a registry value:
<cfregistry action="delete"
branch="HKEY_LOCAL_MACHINE\Software\Macromedia\ColdFusion\CurrentVersion"
entry="test">
This question was written by Jeremy Petersen
It was last updated on February 2, 2006.