2009. május 25., hétfő

How do I create a file association for my win32 application


Problem/Question/Abstract:

How do I create a file association for my win32 application (Update for 98/ME/NT5(2000)/ME) ?

Answer:

In Win32, create a new registry entry under the HKEY_CLASSES_ROOT root key that points to the file extension, the command line to invoke, and the icon to display.

Update:

Windows will execute '\shell\open\command' from the KEY pointed in (Default) value.

So you can :

Clear Default value of extention key with itself ex: .jpg -> .jpg. This make windows use '\shell\open\command' from the proper KEY. (As shown in example)

Create enother key like "MyProgExt" with '\shell\open\command' and point any extension you need to it. This way the extension key default value will be: MyProgExt.

Example:

uses Registry,

procedure TForm1.FileFormatAssociations;
var
  reg: TRegistry;
  FileExt: string;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CLASSES_ROOT;
  reg.LazyWrite := false;

  FileExt := '.jpg';

  //Clear Key - This is important !!!
  reg.OpenKey(FileExt, true);
  reg.WriteString('', FileExt);
  reg.CloseKey;

  //Invoke the program passing the file name as the first parameter
  reg.OpenKey(FileExt + '\shell\open\command', true);
  reg.WriteString('', Application.ExeName + ' "%1"');
  reg.CloseKey;

  //Use the first icon in the executable to display
  reg.OpenKey(FileExt + '\DefaultIcon', true);
  reg.WriteString('', Application.ExeName + ',0');
  reg.CloseKey;

  reg.free;
end;

Nincsenek megjegyzések:

Megjegyzés küldése