2004. szeptember 10., péntek

Get a list of registered files and their extensions


Problem/Question/Abstract:

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:

procedure TForm1.FormShow(Sender: TObject);
var
  K: TRegIniFile;
  i: Integer;
  Extensions: TStringList;
begin
  K := TRegIniFile.Create('');
  K.RootKey := HKEY_LOCAL_MACHINE;
  K.OpenKey('SOFTWARE\MicroSoft\Windows\CurrentVersion\Extensions', False);
  Extensions := TStringList.Create;
  K.GetValueNames(Extensions);
  for i := 0 to Extensions.Count - 1 do
    Memo1.Lines.Add(Extensions.Strings[i] + ' = ' + K.ReadString('',
      Extensions.Strings[i], ''));
  Extensions.Free;
  K.Free;
end;


Solve 2:

Enumerate all extensions and their servers in the registry:

procedure TForm1.Button1Click(Sender: TObject);
var
  reg: TRegistry;
  keys: TStringList;
  i: Integer;
  typename, displayname, server: string;
begin
  memo1.clear;
  reg := TRegistry.Create;
  try
    reg.rootkey := HKEY_CLASSES_ROOT;
    if reg.OpenKey('', false) then
    begin
      keys := TStringlist.create;
      try
        reg.GetKeyNames(keys);
        reg.closekey;
        {memo1.lines.addstrings(keys);}
        for i := 0 to keys.count - 1 do
        begin
          if keys[i][1] = '.' then
          begin
            {this is an extension, get its typename}
            if reg.OpenKey(keys[i], false) then
            begin
              typename := reg.ReadString('');
              reg.closekey;
              if typename <> '' then
              begin
                if reg.OpenKey(typename, false) then
                begin
                  displayname := reg.readstring('');
                  reg.closekey;
                end;
                if reg.OpenKey(typename + '\shell\open\command', false) then
                begin
                  server := reg.readstring('');
                  memo1.lines.add(format('Extension: "%s", Typename: "%s",
                                                                 Displayname:"%s"' + #13#10'  Server: %s', [keys[i],
                                                                 typename, displayname, server]));
                  reg.closekey;
                end;
              end;
            end;
          end;
        end;
      finally
        keys.free;
      end;
    end;
  finally
    reg.free
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése