如何检查outlook是默认的邮件客户端

LaB*_*cca 0 delphi outlook delphi-xe2

我使用此代码来检查outlook是否是默认的邮件客户端:

Function IsOutlookIsDefaultEmailPrg:Boolean;
var
  reg: TRegistry;
  key : string;
begin
  Result := False;
  with TRegistry.Create do
  TRY
    RootKey := HKEY_LOCAL_MACHINE;
    if OpenKeyReadOnly('Software\Clients\Mail') then
    begin
      key := Uppercase(ReadString('')); //default value
    end;
    result :=  (Pos('MICROSOFT OUTLOOK',Key) > 0);
  FINALLY
    Free;
  END;
end;
Run Code Online (Sandbox Code Playgroud)

它一般工作,但在一些电脑上它已被报告不起作用,我检查,注册表密钥在那里.

Pos区分大小写?知道为什么这个有时不起作用吗?还有更好的建议吗?

who*_*ddy 5

我看到您正在使用HKLM密钥来检查默认客户端,但这可以是用户依赖的,因此您应该检查HKCU条目(如果HKCU没有条目,则回退到HKLM).我还删除了With语句并使用了ContainsText(include StrUtilsunit)函数而不是Pos:

function IsOutlookTheDefaultEmailClient:Boolean;
var
  Reg : TRegistry;    
begin
  Result := False;
  Reg := TRegistry.Create;
  try
    // first check HKCU
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKeyReadOnly('Software\Clients\Mail') then
     begin
      Result := ContainsText(Reg.ReadString(''), 'Microsoft Outlook');
      Reg.CloseKey;
     end
    // fall back to HKLM
   else
    begin
     Reg.RootKey := HKEY_LOCAL_MACHINE;
     // this part is susceptible to registry virtualization and my need elevation!
     if Reg.OpenKeyReadOnly('Software\Clients\Mail') then
      begin
       Result := ContainsText(Reg.ReadString(''), 'Microsoft Outlook');
       Reg.CloseKey;
      end;
    end;  
  finally
    Reg.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

编辑

此代码失败的唯一时间是注册表虚拟化发挥作用.这在UAC场景中很常见.如果regedit中存在此密钥,请检查发生故障的计算机:

HKCU\Software\Classes\VirtualStore\MACHINE\SOFTWARE\Clients\Mail
Run Code Online (Sandbox Code Playgroud)

如果是这种情况,您的应用程序将读取此密钥而不是真正的HKLM密钥.唯一的解决方案是提升请求或以管理员身份运行应用程序.