Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening a file in non associated application? 1

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I am using this line to open a .htm file in default application:
Code:
ShellExecute(Handle, 'open', 'F:\my_path\my_file.htm', nil, nil, SW_SHOWNORMAL) ;

This is all very well, it works, except for one of the guys who tested it has associated Notepad++ with htm files. Grrr.

So I need to cater for these events, and open the htm file in the default browser, whether or not htm files are associated with a browser.

I figured the way to do it was detect the default browser and pass that as a string to my line above, using the method as mentioned in this current thread:

So, I found this method of detecting browser:
but this seems an enormous extra amount of code.

Is there an easier way for me to open my htm file in the default browser?

(I did check out the FAQs How do I execute another program from my Delphi application 1 & 2, but couldn't deduce any answers from those)

Steve (Delphi 2007 & XP)
 
The answer actually was on that page you referred to. It was just (evidently) showing many different ways to find it out. This one is simplest.

Then you do as the other thread indicated.

Code:
uses registry;
..
procedure TForm1.Button1Click(Sender: TObject);
var
  Reg: TRegistry;
  KeyName: string;
  ValueStr: string;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CLASSES_ROOT;
    KeyName     := 'htmlfile\shell\open\command';
    if Reg.OpenKey(KeyName, False) then
    begin
      ValueStr := Reg.ReadString('');
      Reg.CloseKey;
      ShowMessage(ValueStr);
    end
    else
      ShowMessage('There isn't a default browser');
  finally
    Reg.Free;
  end;
end;
 
Hmm. If he's associated Notepad++ with html files, doesn't that make Notepad++ the default "browser"? Which means that going through all this rigamarole will result in no change at all.
 
Hmm. If he's associated Notepad++ with html files, doesn't that make Notepad++ the default "browser"?

Not really. I think there's a local and internet association. I tested the code before I posted it by associating HTML with Notepad++ (double-click HTML file, get Notepad++) and then running it and still got the path to internet explorer back out of it.

It seems worth trying, at least.
 
>It seems worth trying, at least.

Definitely.

Cool, I wouldn't have guessed that. I'll have to store this for future reference.
 
Hi Glenn,

Thanks for that gem!
Jeez I wouldn't have thought that.
Even tho I have firefox set as my 'default' browser, the code above gives explorer.

I tried the file association tho, and you are completely correct.
Associate both htm and html with notepad, double click them, opens in notepad. Default 'browser' still explorer.

Think I'll look further into some of the the other code samples I found now.

Thanks again Glenn.


Steve (Delphi 2007 & XP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top