设置默认浏览器的功能其实还是通过修改注册表项来实现,本文的例子中使用了完整的Delphi源码来实现该功能,且不需要重启机器。
百度各种资料会发现,基本上给出的答案都是修改HKEY_CLASSES_ROOT\HTTP\Shell\open\command就可以,可是经过我的多次验证——呵呵,并不行。
于是又重新搜索了各种资料,终于让我找到了:
procedure TForm1.SetDefaultIE;
var
reg: TRegistry;
IEPath: string;
begin
reg := TRegistry.Create;
reg.RootKey := HKEY_LOCAL_MACHINE;
IEPath := GetAppPath('IEXPLORE.EXE');
if IEPath = '' then
begin
log4debug('未找到IE浏览器的安装路径,请检查!');
Exit;
end;
reg.CloseKey;
reg.RootKey := HKEY_CLASSES_ROOT;
try
// 写HKEY_CLASSES_ROOT\http\shell\open\command
if reg.OpenKey('\WebMind', True) then
reg.WriteString('', 'WebMind Document');
reg.CloseKey;
reg.RootKey := HKEY_CLASSES_ROOT;
if reg.OpenKey('\WebMind\Shell\open\command', True) then
begin
reg.WriteString('', '"' + IEPath + '" ' + '"%1"');
reg.CloseKey;
end;
reg.CloseKey;
reg.RootKey := HKEY_CURRENT_USER;
if reg.OpenKey
('\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice',
True) then
reg.WriteString('Progid', 'IE.HTTP');
finally
reg.CloseKey;
reg.Free;
end;
end;