在Delphi上获取IE版本 - 注册表解决方案无效 [英] Get IE version on Delphi - registry solution not working
问题描述
我使用此功能获取IE版本。但在某些情况下它找不到这个键并返回0.还有另一种不依赖注册表的方式吗?
I use this function to get IE version. But in some cases it cannot find this key and returns 0. Is there another way that does not rely on registry ?
function GetIEVersion: string;
var
Reg: TRegistry;
S: string;
i : integer;
begin
Reg := TRegistry.Create;
with Reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('Software\Microsoft\Internet Explorer', False);
if ValueExists('Version') then
begin
S := ReadString('Version');
end
else
begin
S := '0';
CloseKey;
end;
Free;
end;
result := s;
end;
推荐答案
TRegistry.OpenKey()
使用 TRegistry.Access
属性来了解打开密钥时请求的权限。默认情况下, TRegistry.Access
设置为 KEY_ALL_ACCESS
,仅限管理员使用。请改用 TRegistry.OpenKeyReadOnly()
。另外,为了更好的衡量,您可以删除 ValueExists()
检查,因为 ReadString()
返回一个空白字符串,如果该值不存在。
TRegistry.OpenKey()
uses the TRegistry.Access
property to know what permissions to request when opening the key. By default, TRegistry.Access
is set to KEY_ALL_ACCESS
, which is restricted to adminstrators only. Use TRegistry.OpenKeyReadOnly()
instead. Also, for good measure, you can get rid of the ValueExists()
check, as ReadString()
returns a blank string if the value does not exist.
试试这个:
function GetIEVersion: string;
var
Reg: TRegistry;
begin
Result := '';
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('Software\Microsoft\Internet Explorer') then
try
Result := Reg.ReadString('Version');
finally
Reg.CloseKey;
end;
finally
Reg.Free;
end;
if Result = '' then
Result := '0';
end;
这篇关于在Delphi上获取IE版本 - 注册表解决方案无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!