API - descobrir o caminho do programa associado a uma extensao

Top  Previous  Next

// Descobrir o Caminho do Programa Associado a Uma Extensao

 

function PesquisaPrograma(const Ext : string) : string;

var

  Reg: TRegistry;

begin

  Result := '';

  Reg := TRegistry.Create;

  Reg.RootKey := HKEY_CLASSES_ROOT;

  // chave de sistema

  if Reg.OpenKey('.' + ext, False) then

  begin

    Result := Reg.ReadString('');

    Reg.CloseKey;

    if Result <> '' then

    begin

      // pesquisa chave de programa associado

      if reg.OpenKey(Result + '\Shell\Open\Command', False) then Result := Reg.ReadString('');

      Reg.CloseKey;

    end;

  end;

  // tira parâmetros da linha de comando

  if (Result <> ''and (Pos('%', Result) > 0)         then Delete(Result, Pos('%', Result), Length(Result));

  if (Result <> ''and (Result[1] = '"')              then Delete(Result, 11);

  if (Result <> ''and (Result[Length(Result)] = '"') then Delete(Result, Length(Result), 1);

  while (Length(Result) > 0and ((Result[Length(Result)] = ' 'or (Result[Length(Result)] = '"')) do

    Delete(Result, Length(Result), 1);

end;