標籤:style 關聯 trap roo value path adc ati scribe
// 檢測檔案關聯情況// strExt: 要檢測的副檔名(例如: ".txt")// strAppKey: ExeName副檔名在註冊表中的索引值(例如: "txtfile")// 返回TRUE: 表示已關聯,FALSE: 表示未關聯BOOL CheckFileRelation(const char *strExt, const char *strAppKey){ int nRet = FALSE; HKEY hExtKey; char szPath[_MAX_PATH]; DWORD dwSize = sizeof(szPath); if (RegOpenKey(HKEY_CLASSES_ROOT, strExt, &hExtKey) == ERROR_SUCCESS) { RegQueryValueEx(hExtKey, NULL, NULL, NULL, (LPBYTE)szPath, &dwSize); if (_stricmp(szPath, strAppKey) == 0) { nRet = TRUE; } RegCloseKey(hExtKey); return nRet; } return nRet;}//---------------------------------------------------------------------------// 註冊檔案關聯// strExe: 要檢測的副檔名(例如: ".txt")// strAppName: 要相關 App程式名(例如: "C:\MyApp\MyApp.exe")// strAppKey: ExeName副檔名在註冊表中的索引值(例如: "txtfile")// strDefaultIcon: 副檔名為strAppName的表徵圖檔案(例如: "C:\MyApp\MyApp.exe,0")// strDescribe: 檔案類型描述void RegisterFileRelation(char *strExt, char *strAppName, char *strAppKey, char *strDefaultIcon, char *strDescribe){ char strTemp[_MAX_PATH]; HKEY hKey; RegCreateKey(HKEY_CLASSES_ROOT, strExt, &hKey); RegSetValue(hKey, "", REG_SZ, strAppKey, strlen(strAppKey) + 1); RegCloseKey(hKey); RegCreateKey(HKEY_CLASSES_ROOT, strAppKey, &hKey); RegSetValue(hKey, "", REG_SZ, strDescribe, strlen(strDescribe) + 1); RegCloseKey(hKey); sprintf(strTemp, "%s\\DefaultIcon", strAppKey); RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey); RegSetValue(hKey, "", REG_SZ, strDefaultIcon, strlen(strDefaultIcon) + 1); RegCloseKey(hKey); sprintf(strTemp, "%s\\Shell", strAppKey); RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey); RegSetValue(hKey, "", REG_SZ, "Open", strlen("Open") + 1); RegCloseKey(hKey); sprintf(strTemp, "%s\\Shell\\Open\\Command", strAppKey); RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey); sprintf(strTemp, "%s \"%%1\"", strAppName); RegSetValue(hKey, "", REG_SZ, strTemp, strlen(strTemp) + 1); RegCloseKey(hKey);}//測試代碼//增加註冊表關聯 char strExt[10] = ".car"; char strAppKey[30] = "FW_readcar.1.0"; BOOL relationExists = CheckFileRelation(strExt, strAppKey); if (!relationExists) { char strAppName[MAX_PATH + 1] = {0}; strcpy(strAppName,argv[0]); char strDefaultIcon[MAX_PATH + 1] = ""; char strDescribe[100] = "WellTest Interpretation Files"; RegisterFileRelation(strExt, strAppName, strAppKey, strDefaultIcon, strDescribe); }
C++實現添加檔案關聯的方法