Mega Code Archive

 
Categories / Delphi / Examples
 

Get the application associated to an extension

Title: Get the application associated to an extension Question: how do I get the application that opens/executes files with extension ".???" Answer: I wrote a function for this purpose: it returns the application with the full path to where its located, empty if not found Function GetAssociation(Const Fileext: String): String; Var FileClass, Tmp: String; Reg: TRegistry; Begin Result := ''; Reg := TRegistry.Create(KEY_EXECUTE); //read only please Reg.RootKey := HKEY_CLASSES_ROOT; FileClass := ''; If Reg.OpenKeyReadOnly(Fileext) Then Begin FileClass := Reg.ReadString(''); //read the default Reg.CloseKey; End; If FileClass &lt '' Then Begin If Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') Then Begin Result := Reg.ReadString(''); Reg.CloseKey; End; End; Reg.Free; If (Length(Result)0) Then If (Result[1]='"') Then Begin Delete(Result, 1, 1); Result:=Copy(Result, 1, Pos('"', Result)-1) End Else Begin Tmp:=ExtractFileExt(Result); If (Pos(' ', Tmp)0) Then Tmp:=Copy(Tmp, 1, Pos(' ', Tmp)-1); Result:=Copy(Result, 1, Pos(Tmp, Result)+Length(Tmp)) End End; ...ok... then as an example: drop a TEdit and a TButton on your form, put the extension that you need on the TEdit, like: TEdit.Text:='.ZIP' (don't forget to include the "." before the extension) then on the onclick event of the button: Procedure TForm1.Button2Click(Sender: TObject); Begin ShowMessage(GetAssociation(Edit1.Text)) End; that's it keep up coding - The source code of the article was formatted using this unit: PAS 2 HTML converter EberSys