1. 程式人生 > >讀取和設置系統默認瀏覽器

讀取和設置系統默認瀏覽器

creat res tde def mat soft gis then 不同的

系統默認瀏覽器是保存在註冊表中,讀取和設置需要操作註冊表,但是在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;

讀取和設置系統默認瀏覽器