community.borland.com

Article #19646: How to register my program's file type with the Window's registry.

 Technical Information Database

     FAQ: TI4646D - How to register my program's file type with the Window's registry.
Category: Windows API
Platform: All-32Bit
 Product: All32Bit,   

Description:

There are several different ways to do this. One way would be to 
create a .reg file that contains the necessary registry keys, 
and then merge it with the registry. Another way is to do it 
with code using TRegistry. This TI describes how to do it 
with code but it could easily be extended to just a .reg file 
if need be.

////////////////////////
uses Registry, ShlObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  cMyExt = '.abc';
  cMyFileType = 'Project1.FileType';
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    // Set the root key to HKEY_CLASSES_ROOT
    Reg.RootKey := HKEY_CLASSES_ROOT;
    // Now open the key, with the possibility to create
    // the key if it doesn't exist.
    Reg.OpenKey(cMyExt, True);
    // Write my file type to it.
    // This adds HKEY_CLASSES_ROOT\.abc\(Default) = 'Project1.FileType'
    Reg.WriteString('', cMyFileType);
    Reg.CloseKey;
    // Now create an association for that file type
    Reg.OpenKey(cMyFileType, True);
    // This adds HKEY_CLASSES_ROOT\Project1.FileType\(Default)
    //   = 'Project1 File'
    // This is what you see in the file type description for
    // the a file's properties.
    Reg.WriteString('', 'Project1 File');
    Reg.CloseKey;
    // Now write the default icon for my file type
    // This adds HKEY_CLASSES_ROOT\Project1.FileType\DefaultIcon
    //  \(Default) = 'Application Dir\Project1.exe,0'
    Reg.OpenKey(cMyFileType + '\DefaultIcon', True);
    Reg.WriteString('', Application.ExeName + ',0');
    Reg.CloseKey;
    // Now write the open action in explorer
    Reg.OpenKey(cMyFileType + '\Shell\Open', True);
    Reg.WriteString('', '&Open');
    Reg.CloseKey;
    // Write what application to open it with
    // This adds HKEY_CLASSES_ROOT\Project1.FileType\Shell\Open\Command
    //  (Default) = '"Application Dir\Project1.exe" "%1"'
    // Your application must scan the command line parameters
    // to see what file was passed to it.
    Reg.OpenKey(cMyFileType + '\Shell\Open\Command', True);
    Reg.WriteString('', '"' + Application.ExeName + '" "%1"');
    Reg.CloseKey;
    // Finally, we want the Windows Explorer to realize we added
    // our file type by using the SHChangeNotify API.
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  finally
    Reg.Free;
  end;
end;
////////////////////////



Reference:

None

6/21/99 2:10:19 PM
 

Last Modified: 01-SEP-99