How to Register / Unregister File Types

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;

Tags: , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login