Setting Registry Keys and Values

Besides retrieving registry keys and values, the cfregistry tag can also create new registry keys as well as set and update registry values. This allows you, for example, to store application-specific information in the system registry as opposed to in a database. It also allows you to add/update ODBC data sources that normally must be added/updated through the Windows Control Panel’s ODBC Data Source Administrator. Example 25-4 shows how to add a key called MyDataSource to the registry.

Example 25-4. Creating a registry key

<!--- This creates a new key called MyDataSource under the ODBC.INI branch --->
<cfregistry action="Set"
          branch="HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI"
          type="Key" entry="MyDataSource">
 
<!--- Query the branch so that we can show it was added --->            
<cfregistry action="GatAll"
          branch="HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI"
          name="GetDSNs" type="Any" sort="Asc">
 
<h3>HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI</h3>
 
<table border="1">
<tr>
  <th>Entry</th><th>Type</th><th>Value</th>
</tr>
 
<cfoutput query="GetDSNs">
<tr>
  <td>#Entry#</td><td>#Type#</td><td>#Value#</td>
</tr>
</cfoutput>
</table>

Example 25-4 uses the cfregistry tag to create a new registry key called MyDataSource by setting the action attribute to Set. branch specifies the registry branch to create the new key under. We decide to create our new key under the registry branch:

HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI

The type attribute is optional and specifies the ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.