如何设置程序与文件关联并且双击关联文件时获取文件的路径呢?
一般来说可以通过写注册表的方式实现,在函数入口处实现功能。
例如在VC/MFC中,可以在应用程序的C**App.cpp文件中InitInstance()函数中实现该功能。
#include <string>
using namespace std;
//关联文件的后缀名,如"txt"、"doc"等
string m_csExtension;string m_csShellOpenCommand;
string m_csDocumentShellOpenCommand;
//注册表中文件夹类名
string m_csDocumentClassName;
//关联文件的默认图标string m_csDocumentDefaultIcon;
///赋值函数//
void SetExtension( LPCTSTR szExtension ){m_csExtension = szExtension;}
void SetShellOpenCommand( LPCTSTR szShellOpenCommand )
{m_csShellOpenCommand = szShellOpenCommand;}
void SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
{m_csDocumentShellOpenCommand = szDocumentShellOpenCommand;}
void SetDocumentClassName( LPCTSTR szDocumentClassName )
{m_csDocumentClassName = szDocumentClassName;}
void SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
{m_csDocumentDefaultIcon = szDocumentDefaultIcon;}
///赋值函数//
//关键函数:实现写注册表的函数
BOOL SetRegistryValue(HKEY hOpenKey,LPCTSTR szKey,LPCTSTR szValue,LPCTSTR szData){// validate inputif( !hOpenKey || !szKey || !szKey[0] ||!szValue || !szData ){::SetLastError(E_INVALIDARG);return FALSE;}
BOOL bRetVal = FALSE;DWORD dwDisposition;DWORD dwReserved = 0;HKEY hTempKey = (HKEY)0;
// length specifier is in bytes, and some TCHAR// are more than 1 byte eachDWORD dwBufferLength = lstrlen(szData) * sizeof(TCHAR);
// Open key of interest// Assume all access is okay and that all keys will be stored to file// Utilize the default security attributesif( ERROR_SUCCESS == ::RegCreateKeyEx(hOpenKey, szKey, dwReserved,(LPTSTR)0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0,&hTempKey, &dwDisposition) ){// dwBufferLength must include size of terminating nul// character when using REG_SZ with RegSetValueEx functiondwBufferLength += sizeof(TCHAR);if( ERROR_SUCCESS == ::RegSetValueEx(hTempKey, (LPTSTR)szValue,dwReserved, REG_SZ, (LPBYTE)szData, dwBufferLength) ){bRetVal = TRUE;}}
// close opened keyif( hTempKey ){::RegCloseKey(hTempKey);}
return bRetVal;}
BOOL RegSetExtension(void){if( m_csExtension.empty() ){return FALSE;}
std::string csKey = "." + m_csExtension;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csDocumentClassName.c_str());
if( !m_csShellOpenCommand.empty() ){csKey += "//shell//open//command";SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csShellOpenCommand.c_str());}
return TRUE;}
BOOL RegSetDocumentType(void){if( m_csDocumentClassName.empty()){return FALSE;}
std::string csKey = m_csDocumentClassName;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csDocumentDescription.c_str());
// DefaultIconif( !m_csDocumentDefaultIcon.empty() ){csKey = m_csDocumentClassName;csKey += "//DefaultIcon";SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csDocumentDefaultIcon.c_str());}
// shell/open/commandif( !m_csShellOpenCommand.empty() ){csKey = m_csDocumentClassName;csKey += "//shell//open//command";SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csShellOpenCommand.c_str());}
return TRUE;}
BOOL RegSetAllInfo(void){RegSetExtension();RegSetDocumentType();
return TRUE;}
//注册程序与文件后缀名的关联void RegisterFileAndProgram(){
一个应用程序与多个文件后缀关联#define strExternsionLength 4
LPCTSTR strExtension[] ={"bmp","jpg","jpe","jpeg"};
CGCFileTypeAccess TheFTA;
TCHAR szProgPath[MAX_PATH * 2];
//获取程序路径
::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
CString csTempText;
for(int i = 0; i < strExternsionLength; ++i){
//设置程序需要关联的后缀名,如"txt" "doc" 等
SetExtension(strExtension[i]);
csTempText.Format("/"%s/" %s",szProgPath,"/"%1/"");
SetShellOpenCommand(csTempText);
SetDocumentShellOpenCommand(csTempText);
//设置注册表中文件类的别名,例如可以是程序名称:**.exe
SetDocumentClassName("**");
// use first icon in program
csTempText = szProgPath;
csTempText += ",0";
SetDocumentDefaultIcon(csTempText);
RegSetAllInfo();
}
}
//入口函数:初始化所需的操作
BOOL C**App::InitInstance()
{
//
//***Code***
//注册程序与文件后缀名的关联RegisterFileAndProgram();
// 分析标准外壳命令、DDE、打开文件操作的命令行CCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);//获取命令行传过来的参数:双击打开的文件的文件路径名称CString strFilePathName = cmdInfo.m_strFileName;
// 调度在命令行中指定的命令。如果// 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。if (!ProcessShellCommand(cmdInfo))return FALSE;
//
//通过获取的strFilePathName文件名称实现相关操作//
//Add code here//
return TRUE;
}