1. Home
  2. Computing & Technology
  3. Delphi Programming

Associate a filetype (extension) with your application

By , About.com Guide

When creating applications, it's often necessary to store information as a file on the hard drive. You may choose to create your own extension and then associate that extension with your application through Windows.

Basically, you need to add two keys to the registry under HKEY_CLASSES_ROOT. Say your extension is ".ext", then the first key you add is the extension itself:

HKEY_CLASSES_ROOT\.ext\

and set the "default" string value of this key to an "internal name" for your file type - for example MyApp.Document:

HKEY_CLASSES_ROOT\.ext\Default = "MyApp.Document"

You then create another key with this name:

HKEY_CLASSES_ROOT\MyApp.Document\

Create a sub-key of this called "shell", a sub-key of *this* called "open" and a further sub-key of "open" called "command". The default value under this key is the location and name of your application followed by "%1" which represents the filename parameter that Windows will pass to your executable:

HKEY_CLASSES_ROOT\MyApp.Document\shell\open\command\Default = "C:\myapp\myapp.exe %1"

You can do this in code with the TRegistry object, or use InstallShield, which can make registry changes for you. I'd advise doing both, in case the user trashes your registry entry.

p.s.
To work with registry from Delphi, read: Delphi and Registry.

How to Run your Application by Double-Clicking on a File (Register Extension)

Delphi tips navigator:
» Label: FocusControl
« Summing multiple selected rows in a DBGrid

Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2000 Delphi Tips
  7. Associate a filetype (extension) with your application

©2010 About.com, a part of The New York Times Company.

All rights reserved.