Link to home
Start Free TrialLog in
Avatar of zicodt
zicodt

asked on

Register a file type for my program...

Hi all,

I would like to create a button on my program to register a file type for it...

The file extension is .nfo, and I would like to when I click the icon in windows my program opens...

Thanks for the help...
Avatar of mirghani
mirghani

Put this code in your project's OnCreate event,
the application you want to be opened when
double clicking on .nfo file:

// Add Registry to uses clause, the post this code:

var
  Regs: TRegistry;
begin
  Regs:= TRegistry.Create;
  Regs.RootKey:= HKEY_CLASSES_ROOT;

  (*** Check if extension already registred ***)
  if Regs.OpenKey('.nfo', False) then
  begin
    Regs.CloseKey;
    Regs.Free;
    Exit; (*>>>>>>>>>>>>>>>>*)
  end;

  Regs.OpenKey('\.nfo', True);
  Regs.WriteString('', 'YourProject.Data');
  Regs.OpenKey('\YourProject.Data\Shell\Open\Command', True);
  Regs.WriteString('', ParamStr(0) + ' %1');

  (*** Select an Icon for .esn files ***)
  Regs.OpenKey('\YourProject.Data\DefaultIcon', True);
  Regs.WriteString('', ParamStr(0) + ',0'); // First Icon in exe
  Regs.CloseKey;
  Regs.Free;

end;


Motaz
Sorry, I forget to logout from mirghani account
ASKER CERTIFIED SOLUTION
Avatar of Motaz
Motaz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zicodt

ASKER

Okay thanks for replying motaz...

I got an error tough, I took a screenshot of the code for you to see... copy this address on the clipboard and paste on your browser to see the error: http://www.tempdir.hpg.com.br/temp/error.jpg

In the code you provided, the "YourProjectData", I need to change it to my project folder name?

Thanks again...
Generally, the idea is an expert provides you with a portion of source code to help you get going - not write the whole program for you.  

What Motaz provided you was a portion of a procedure which you would copy into your application.  Where you place it is upto you, but under normal situations, it would be placed in the FormCreate procedure.

procedure TForm1.FormCreate(Sender: TObject);
var
  Regs: TRegistry;
begin
  Regs:= TRegistry.Create;
  Regs.RootKey:= HKEY_CLASSES_ROOT;
  (* extra code here*)
  Regs.Free;
end;

Just remember, part of learning is trial and error.  Have a look at what people post and see if you can figure out the error instead of asking straight away.  Especially for low-point questions.  People are happy to help, but you must also attempt to help yourself.

Good luck!

Stuart.


Extra note:
The use of try..finally blocks should be used to ensure the registry object is freed.
Avatar of zicodt

ASKER

Yeah... I'm just starting on delphi... trying to learn on myself... but thanks for all the help...

I'm new to this forum... I know that I can "Accept comment as answer" and give you a reward, but since both of you helped me how can I reward both of you?
Motaz provided you with a working solutions - he deserves the points.  I just added a little bit extra info to it.  So, it's cool.  Save your points for your next question.
Avatar of zicodt

ASKER

Okay, thanks for explaining me about the forum, I wasn't quite sure about this points thingy, that's why I gave a low-point question... now I'm aware of it.. thanks again...
The points were OK for what you were asking - it wasn't an overly complicated question.
Thanks Johnson for helping zicodt, and thanks zicodt for the points
Motaz