我认为这并不是“黑客攻击”注册表的行为。据我所知,如果您想要在不读取注册表中的任何值的情况下完成您想要做的事情,没有好的方法可用。
因此,如果您想使用注册表,请使用以下代码:
uses Registry;
function GetProgramAssociation(const Ext: string): string;
var reg: TRegistry;
s: string;
begin
s:='';
reg:=TRegistry.Create;
try
reg.RootKey:=HKEY_CLASSES_ROOT;
if reg.OpenKey('.'+ext+'shellopencommand', false) then
begin
s:=reg.ReadString('');
reg.CloseKey;
end
else
begin
if reg.OpenKey('.'+ext, false) then
begin
s:=reg.ReadString('');
reg.CloseKey;
if s='' then
begin
if reg.OpenKey(s+'shellopencommand', false) then
s:=reg.ReadString('');
reg.CloseKey;
end;
end;
end;
if Pos('%', s) > 0 then Delete(s, Pos('%', s), length(s));
if ((length(s)>0) and (s[1]='"')) then Delete (s, 1, 1);
if ((length(s)>0) and (s[length(s)]='"')) then Delete(s, Length(s), 1);
while ((length(s)>0) and ((s[length(s)]=#32) or (s[length(s)]='"'))) do
Delete(s, Length(s), 1);
result:=s;
finally
reg.Free;
end;
end;
然后:
if GetProgramAssociation(Extension) = '' then
ShowMessage('Nope!');
它运行良好。
如果扩展名没有与有效程序相关联,则返回空字符串。
例如,如果输入“doc”(不带“.”),则返回Word.Document.8;如果输入“abcdef”,则返回空字符串('')。
请记住:输入扩展名时不要加点。