Author: William Gerbert
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:
1 { ... }
2 var
3 Regist: TRegistry;
4 begin
5 Result
6 Regist := TRegistry.Create;
7 try
8 Regist.RootKey := HKEY_CLASSES_ROOT;
9 {file type}
10 if Regist.OpenKey('.xyz' {= your extension}, True) then
11 begin
12 Regist.WriteString('', 'xyz-file' {= unique name});
13 Regist.CloseKey;
14 end;
15 {name}
16 if Regist.OpenKey('xyz-file' {= same unique name}, True) then
17 begin
18 Regist.WriteString('', 'xyz super file'
19 {= short description, is shown in explorer});
20 Regist.CloseKey;
21 end;
22 {icon}
23 if Regist.OpenKey('xyz-file\DefaultIcon', True) then
24 begin
25 {third icon of your exe, 0 is the main icon I think, of course you can use
26 other files than Application.ExeName}
27 Regist.WriteString('', Application.ExeName + ', 3');
28 Regist.CloseKey;
29 end;
30 {open}
31 if Regist.OpenKey('xyz-file\Shell\Open\Command', True) then
32 begin
33 Regist.WriteString('', Application.ExeName + ' "%1"');
34 {or other/ additional parameters}
35 Regist.CloseKey;
36 Result := True;
37 end;
38 {you can add more for edit, print etc.}
39 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
40 {tell Windows we have done it}
41 finally
42 Regist.Free;
43 end;
44 end;
|