2008. április 27., vasárnap

Register your own file extensions in the Windows registry


Problem/Question/Abstract:

I have an application that create files. I want those files to be associated to my application so that when you double click on those files will launch my application and open the particular file. How do I do this?

Answer:

Take a look at the registry (HKEY_CLASSES_ROOT) to see what exactly is possible. Basically, you have to add an entry that equals the file extension, one that equals an unique name and the action. And you have to tell Windows that you have registered a new extension. Something like:

{ ... }
var
  Regist: TRegistry;
begin
  Result
    Regist := TRegistry.Create;
  try
    Regist.RootKey := HKEY_CLASSES_ROOT;
    {file type}
    if Regist.OpenKey('.xyz' {= your extension}, True) then
    begin
      Regist.WriteString('', 'xyz-file' {= unique name});
      Regist.CloseKey;
    end;
    {name}
    if Regist.OpenKey('xyz-file' {= same unique name}, True) then
    begin
      Regist.WriteString('', 'xyz super file'
        {= short description, is shown in explorer});
      Regist.CloseKey;
    end;
    {icon}
    if Regist.OpenKey('xyz-file\DefaultIcon', True) then
    begin
      {third icon of your exe, 0 is the main icon I think, of course you can use
                        other files than Application.ExeName}
      Regist.WriteString('', Application.ExeName + ', 3');
      Regist.CloseKey;
    end;
    {open}
    if Regist.OpenKey('xyz-file\Shell\Open\Command', True) then
    begin
      Regist.WriteString('', Application.ExeName + ' "%1"');
        {or other/ additional parameters}
      Regist.CloseKey;
      Result := True;
    end;
    {you can add more for edit, print etc.}
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
    {tell Windows we have done it}
  finally
    Regist.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése