laoxia

读取和设置系统默认浏览器

系统默认浏览器是保存在注册表中,读取和设置需要操作注册表,但是在xp和win7(Vista及以上系统)下的位置是不同的,需要分别读取和设置;

读取系统默认浏览器:

Win7下:

function GetDefExplorerPathOnWin7(): string;
const
CPath = \'%s\shell\open\command\';

var
oReg: TRegistry;
sKey, sPath: string;
begin
Result := \'\';
sKey := \'\';

oReg := TRegistry.Create();
try
oReg.RootKey := HKEY_CURRENT_USER;
if oReg.OpenKeyReadOnly(\'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice\') then
begin
sKey := Trim(oReg.ReadString(\'Progid\'));
end;

if not SameText(sKey, \'\') then
begin
oReg.CloseKey;

oReg.RootKey := HKEY_CLASSES_ROOT;
sPath := Format(CPath, [sKey]);
if oReg.OpenKeyReadOnly(sPath) then
begin
Result := Trim(oReg.ReadString(\'\'));
end;
end;
finally
oReg.CloseKey;
oReg.Free;
end;
end;

 

XP系统下:

function GetDefExplorerPathOnXP(): string;
var
oReg: TRegistry;
begin
Result := \'\';

oReg := TRegistry.Create();
try
oReg.RootKey := HKEY_CLASSES_ROOT;
if oReg.OpenKeyReadOnly(\'HTTP\shell\open\command\') then
begin
Result := Trim(oReg.ReadString(\'\'));
end;
finally
oReg.CloseKey;
oReg.Free;
end;
end;

 

设置系统默认浏览器:

这里假设设置系统默认浏览器为IE,先将IE的路径读取到变量ABrowserPath中;

win7系统下:

procedure SetDefaultBrowerOnWin7();
var
oReg: TRegistry;
begin
oReg := TRegistry.Create();
try
oReg.RootKey := HKEY_CURRENT_USER;
if oReg.OpenKey(\'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice\', True) then
begin
oReg.WriteString(\'Progid\', \'IE.HTTP\');
end;

oReg.CloseKey();
finally
oReg.Free;
end;
end;

 

XP系统下:

procedure SetDefaultBrowerOnXP(const ABrowserPath: string);
var
oReg: TRegistry;
sValue: string;
begin
oReg := TRegistry.Create();
try
oReg.RootKey := HKEY_CLASSES_ROOT;
if oReg.OpenKey(\'HTTP\shell\open\command\', True) then
begin
sValue := Format(\'"%s" -- "%%1"\', [ABrowserPath]);
oReg.WriteString(\'\', sValue);
end;

oReg.CloseKey();
finally
oReg.Free;
end;
end;

分类:

技术点:

相关文章: