检索与文件关联的应用程序? [英] Retrieve application associate with a file?

查看:92
本文介绍了检索与文件关联的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Windows上,给定一个文件(带有文件路径),如何检索用于打开该文件的程序?

例如,给定一个.PDF文件,我的程序将知道该文件应由PDF查看器处理,并向我返回安装在本机中的Adobe Reader的文件路径.

或给定一个.DOC文件,我的程序将知道该文件应由MS Word处理,并为我提供安装在某处的MS Word的文件路径...

可以使用2个命令"assoc"和"ftype"从控制台检索此信息.但是我不知道如何从我的C程序中做到这一点.

我环顾了很久,但没有得到答案.任何帮助将不胜感激.

谢谢,
NX

Hi,

On Windows, given a file (with its filepath), how can I retrieve the program used to open it?

For example, given a .PDF file, my program would know that this file should be handled by a PDF viewer, and return mes the filepath of the Adobe Reader installed in my machine.

Or given a .DOC file, my program would know that this file should be handled by MS Word, and gives me the filepath of the MS Word installed somewhere ...

This information can be retrieved from console, with 2 commands "assoc" and "ftype". But I dont know how to do so from my C program.

I looked around for a long time, but got no answer. Any help would be highly appreciated.

Thanks,
NX

推荐答案

让我们假设您知道如何读取注册表.您可以在这里获得所需的所有功能:
http://msdn.microsoft.com/en-us/library/ms724875.aspx [ ^ ].

假设您具有扩展名".ext"(用开头的点替换包含扩展名参数的变量的值).我将在扩展名为".pdf"的示例中使用我的系统来说明该过程.
这是伪代码:
Let''s assume you know how to read the registry. You can get all the functions you need here:
http://msdn.microsoft.com/en-us/library/ms724875.aspx[^].

Let''s say you have the extension ".ext" (substitute the value of your variable holding the extension parameter, with starting dot). I will illustrate the procedure using my system on the example of the extension ".pdf".
Here is the pseudo-code:

  1. 获取注册表项HKEY_CLASSES_ROOT\.pdf
  2. 获取该注册表项的默认值;分配给某些变量key_default.在我的系统上,key_default =="AcroExch.Document"
  3. 获取注册表项HKEY_CLASSES_ROOT\,并将其与key_default连接.在我的系统上,它是"HKEY_CLASSES_ROOT \ AcroExch.Document"
  4. 通过添加"\Shell\Open\Command"(从右连接)转到上一步中找到的键的子键.在我的系统上,它是"HKEY_CLASSES_ROOT \ AcroExch.Document \ Shell \ Open \ Command".
  5. 获取此键的默认值.这是一个命令行,用于使用命令行管理程序打开具有已注册扩展名的文件.在我的系统上,它是Adobe acrobat查看器的完整路径:".. \\ AcroRd32.exe""%1".参数"%1"以Windows批处理文件的格式给出.这意味着,如果将整个字符串放在批处理文件中,则可以使用代表完整数据文件名的参数来调用此批处理.该文件的类型应为您使用的扩展名定义的类型.
  6. 要使用注册了给定文件扩展名的应用程序加载文件,您需要使用路径名和参数说明符%1"替换为实际文件名.

  1. Get to the registry key HKEY_CLASSES_ROOT\.pdf
  2. Get default value for this key; assign to some variable key_default. On my system, key_default == "AcroExch.Document"
  3. Get to the registry key HKEY_CLASSES_ROOT\, concatenate it with key_default. On my system, it is "HKEY_CLASSES_ROOT\AcroExch.Document"
  4. Go to the sub-key of the key found on the previous step by adding "\Shell\Open\Command" (concatenate from right). On my system it''s "HKEY_CLASSES_ROOT\AcroExch.Document\Shell\Open\Command".
  5. Get default value for this key. This is a command line used to open the file with the registered extension using the Shell. On my system, it is a full path the Adobe acrobat viewer: "..\\AcroRd32.exe" "%1". The parameter "%1" is given in the format of Windows batch file. It means that if you put the whole string in the batch file, you can call this batch with the parameter representing the full data file name; the file should be of the type defined by the extension you''ve started with.
  6. To load the file with the application registered for a give file extension you need to run the application using the path name with the parameter specifier "%1" substituted with the actual file name.



问题解决了.

—SA



Problem solved.

—SA


快速,简短,简单而又不错的方法是在文件上使用FindExecutable().因此,无需弄乱注册表:-)

谢谢,
N
The quick, short, easy and nice way is to use FindExecutable() on the file. So there is no need to mess up with the registries :-)

Thanks,
N


来看看 http://msdn.microsoft.com/en-us/library/ee872121(v = VS.85).aspx [
have a look in and around http://msdn.microsoft.com/en-us/library/ee872121(v=VS.85).aspx[^]

It talks about file and application registration in the shell


这篇关于检索与文件关联的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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