Associate File Extension with Shell OPEN command and Application(转)

Introduction
This source contains a class CGCFileTypeAccess that can associate a file extension with a program.

The association allows a new instance of the program to be activated when the icon is clicked upon in the Windows Explorer window. The full file path to the file will be sent to the program on the command line.

The Open command also appears on the shell context menu for the file type. Selection of the open command from the context menu sends the file on the command line to a new instance of the program.

An example of associating a file extension using the class would be done as follows, and can be found in the InitInstance of the demo program:

Hide Copy Code
CGCFileTypeAccess TheFTA;

// get full file path to program executable file
TCHAR szProgPath[MAX_PATH * 2];
::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));

CString csTempText;

TheFTA.SetExtension(“CGFILETYPETEST”);

// just pass file path in quotes on command line
csTempText = szProgPath;
csTempText += ” \”%1\”“;
TheFTA.SetShellOpenCommand(csTempText);
TheFTA.SetDocumentShellOpenCommand(csTempText);

TheFTA.SetDocumentClassName(“CGFileType.Document”);

// use first icon in program
csTempText = szProgPath;
csTempText += “,0”;
TheFTA.SetDocumentDefaultIcon(csTempText);

// set the necessary registry entries
TheFTA.RegSetAllInfo();
The relevant registry entries are all under HKEY_CLASSES_ROOT. The two primary root keys are .’FileExtension’ and ‘Document Class Name’. Keys and values underneath these two primary keys control the file association and the shell open command.

The demo program will execute the file association code listed above. After it has been run at least once, each time you ‘open’ one of the associated file types, the program will display the file path from the command line in a message box.

License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值