Home > General > How to Register / Unregister File Types

How to Register / Unregister File Types

December 15th, 1999 Leave a comment Go to comments

Sometimes you want your program to associate itself with a certain file type. Well, there are 2 ways to do so: 1) Use a VCL component to do the job. 2) Make the changes to the Registry yourself. This is the better way. Here’s the code to do so:

  uses
    Registry;
  ...
  procedure RegisterExtension(const sExt, sDesc: string);
  begin
    with TRegistry.Create do
      try
        RootKey := HKEY_CLASSES_ROOT;
        if OpenKey(sExt, True) then begin
          WriteString('', sDesc);
          CloseKey;
        end;
        if OpenKey('' + sDesc, True) then begin
          WriteString('', sDesc);
          CloseKey;
        end;
        if OpenKey('DefaultIcon', True) then begin
          WriteString('', Application.ExeName + ',' + IntToStr(sIconIndex));
          CloseKey;
        end;
        if OpenKey('' + sDesc + 'ShellOpenCommand', True) then begin
          WriteString('', '"' + Application.ExeName + '" "%1"');
          CloseKey;
        end;
      finally
        Free;
      end;
  end;
Categories: General Tags: , , , ,
  1. No comments yet.