如何使用Delphi获取与文件扩展名关联的程序名称? [英] How can I get the Name of the Program associated with a file extension using Delphi?

查看:253
本文介绍了如何使用Delphi获取与文件扩展名关联的程序名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取当前用户当前与文件扩展名相关联的程序的名称。如果您右键单击文件并选择属性,那么我需要的是打开行右侧的程序名称。



例如对于.xls,我想要得到答案Microsoft Office Excel,或用户使用的默认程序打开.xls文件的任何程序。



我确定它不像进入HKEY_CLASSES_ROOT并选择它那么容易,因为它也可以在HKEY_LOCAL_MACHINE或HKEY_CURRENT_USER或HKEY_USERS中指定。所有我需要知道的是Windows使用的啄食顺序来确定这一点,以及如何到达每个位置。当然,这样做的Windows API调用将是理想的。



这是一个类似的问题:
如何使用Delphi从文件扩展名获取图标和描述?,但这个问题只能解释如何获取相关程序的扩展名和图标的描述。我找不到一种扩展方式来获取相关程序的名称。



我使用的是Delphi 2009,需要一个适用于Windows XP的解决方案,Vista和7。






感谢大家的答案。



似乎我认为可执行文件的名称毕竟不在注册表中。而且,在广泛地看到一个将提供名称的Windows API之后,我找不到一个。



我认为Mef的答案是最好的。从程序的可执行文件中包含的信息中获取可执行文件的名称。

解决方案

步骤1



获取分配给文件扩展名的可执行文件,例如使用以下功能:

 使用注册表,Windows,SysUtils; 

函数GetAssociation(const DocFileName:string):string;
var
FileClass:string;
注册:TRegistry;
begin
结果:='';
注册:= TRegistry.Create(KEY_EXECUTE);
Reg.RootKey:= HKEY_CLASSES_ROOT;
FileClass:='';
如果Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)))然后
begin
FileClass:= Reg.ReadString('');
Reg.CloseKey;
结束
如果FileClass<> ''然后开始
如果Reg.OpenKeyReadOnly(FileClass +'\Shell\Open\Command')然后
开始
结果:= Reg.ReadString('');
Reg.CloseKey;
结束
结束
Reg.Free;
结束

(请参阅 here ,或marc_s'anwser to this question: - )



步骤2



现在,您可以从该可执行文件的版本信息中读出程序的名称!最简单的方法是使用可以通过Google找到的TVersionInfo类,例如 here

  var VersionInfo:TVersionInfo; 
VersionInfo:= TVersionInfo.Create('PathToExe\\\
ame.exe');
s:= VersionInfo.KeyValue ['Description'];

但是,您必须注意某些程序使用描述键(如RAD Studio本身或MS Excel),而其他则使用产品名称键...


I need to get the name of the program currently associated with a file extension for the current user. If you right-click on a file and select properties, then what I need is the program name that is to the right of the "Opens with" line.

e.g. For ".xls", I want to be able to get the answer "Microsoft Office Excel", or whatever program the user has as their default program to open .xls files.

I have determined it's not as easy as just going into HKEY_CLASSES_ROOT and picking it out, since it may also be specified in HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER or HKEY_USERS.

Maybe all I need to know is the pecking order used by Windows to determine this and how to get to each of the locations. Of course, a Windows API call to do this would be ideal.

This is a similar question to: How to get icon and description from file extension using Delphi? but that question only answered how to get the description of the extension and the icon of the associated program. I couldn't find a way to extend that to also get the name of the associated program.

I'm using Delphi 2009 and need a solution that works on Windows XP, Vista and 7.


Thank you all for your answers.

It appears my belief that the name of the executable is not in the Registry after all. And after looking around extensively for a Windows API that will give the name, I could not find one.

I think Mef's answer then is the best. To get the name of the executable from the information included in the program's executable.

解决方案

Step 1

Get the executable which is assigned to a file extension, for instance with the following function:

uses Registry, Windows, SysUtils;

function GetAssociation(const DocFileName: string): string;
var
  FileClass: string;
  Reg: TRegistry;
begin
  Result := '';
  Reg := TRegistry.Create(KEY_EXECUTE);
  Reg.RootKey := HKEY_CLASSES_ROOT;
  FileClass := '';
  if Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)) then
  begin
    FileClass := Reg.ReadString('');
    Reg.CloseKey;
  end;
  if FileClass <> '' then begin
    if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then
    begin
      Result := Reg.ReadString('');
      Reg.CloseKey;
    end;
  end;
  Reg.Free;
end;

(See here, or marc_s' anwser to this question :-)

Step 2

Now you can read out the name of the program from the version information of this executable! The easiest way is using the TVersionInfo class you can find via Google, for instance here.

var VersionInfo: TVersionInfo;  
VersionInfo := TVersionInfo.Create('PathToExe\name.exe');  
s := VersionInfo.KeyValue['Description'];

However, you have to be aware that some programs use the description key therefore (like RAD Studio itself or MS Excel), while others use the product name key...

这篇关于如何使用Delphi获取与文件扩展名关联的程序名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆