zoukankan      html  css  js  c++  java
  • C++实现添加文件关联的方法

    // 检测文件关联情况
    // 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: 要关联的应用程序名(例如: "C:MyAppMyApp.exe")
    // strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
    // strDefaultIcon: 扩展名为strAppName的图标文件(例如: "C:MyAppMyApp.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);
            }
  • 相关阅读:
    ztCreateUserWizard输入密码和设置安全问题
    寻觅在office(确切的说是word) 的工具栏中添加控件的方法,找到了这个控件列表
    vs2005,combox 数据绑定和SelectedIndexChanged事件触发 引发的问题
    今天下载安装了Enterprise Library for .NET Framework 2.0 January 2006,准备试试dataAccess application block
    zt 3DO的历史,让人感慨啊
    iBM,tivoli,flash挺有意思的
    zt科学家发现自转最快中子星 比地球快1亿倍
    c#中读取应用程序路径的方法
    郁闷啊郁闷,为了access的like,折腾了一上午.
    人生
  • 原文地址:https://www.cnblogs.com/nalanhairuo/p/7668410.html
Copyright © 2011-2022 走看看