Author: William Gerbert
Is there a Windows API that returns the name of the program that a particular file
extension is associated with?
Answer:
Solve 1:
To get a list of the applications and their extensions for opening up files in
Windows95 do the following:
1
2 procedure TForm1.FormShow(Sender: TObject);
3 var
4 K: TRegIniFile;
5 i: Integer;
6 Extensions: TStringList;
7 begin
8 K := TRegIniFile.Create('');
9 K.RootKey := HKEY_LOCAL_MACHINE;
10 K.OpenKey('SOFTWARE\MicroSoft\Windows\CurrentVersion\Extensions', False);
11 Extensions := TStringList.Create;
12 K.GetValueNames(Extensions);
13 for i := 0 to Extensions.Count - 1 do
14 Memo1.Lines.Add(Extensions.Strings[i] + ' = ' + K.ReadString('',
15 Extensions.Strings[i], ''));
16 Extensions.Free;
17 K.Free;
18 end;
Solve 2:
Enumerate all extensions and their servers in the registry:
19
20 procedure TForm1.Button1Click(Sender: TObject);
21 var
22 reg: TRegistry;
23 keys: TStringList;
24 i: Integer;
25 typename, displayname, server: string;
26 begin
27 memo1.clear;
28 reg := TRegistry.Create;
29 try
30 reg.rootkey := HKEY_CLASSES_ROOT;
31 if reg.OpenKey('', false) then
32 begin
33 keys := TStringlist.create;
34 try
35 reg.GetKeyNames(keys);
36 reg.closekey;
37 {memo1.lines.addstrings(keys);}
38 for i := 0 to keys.count - 1 do
39 begin
40 if keys[i][1] = '.' then
41 begin
42 {this is an extension, get its typename}
43 if reg.OpenKey(keys[i], false) then
44 begin
45 typename := reg.ReadString('');
46 reg.closekey;
47 if typename <> '' then
48 begin
49 if reg.OpenKey(typename, false) then
50 begin
51 displayname := reg.readstring('');
52 reg.closekey;
53 end;
54 if reg.OpenKey(typename + '\shell\open\command', false) then
55 begin
56 server := reg.readstring('');
57 memo1.lines.add(format('Extension: "%s", Typename: "%s",
58 Displayname:"%s"'keys[i],
59 typename, displayname, server]));
60 reg.closekey;
61 end;
62 end;
63 end;
64 end;
65 end;
66 finally
67 keys.free;
68 end;
69 end;
70 finally
71 reg.free
72 end;
73 end;
|