zoukankan      html  css  js  c++  java
  • C++实现文件关联

      下面这段话是百度百科对文件关联的解释。

      文件关联就是将一种类型的文件与一个可以打开它的程序建立起一种依存关系。举个例子来说,位图文件(BMP文件)在Windows中的默认关联程序画图,如果将其默认关联改为用ACDSee程序来打开,那么ACDSee就成了它的默认关联程序。

      实际上就是设置一种文件的默认打开方式。

      文件关联的信息都写在注册表里,需要写入/修改注册表信息来实现文件关联。

      下面给出如何设置一种文件的默认打开程序及显示的图标的代码。

      文件关联的两个简单函数:

    // 检测文件关联情况
    
    // 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] = ".REC";
    
    char strAppKey[20] = "WellTest.REC.1.0";         
    
    BOOL relationExists = CheckFileRelation(strExt, strAppKey);
    
    if(!relationExists)
    
    {
      char strAppName[MAX_PATH + 1] = "C:\WellTest\trunk\bin\WellTest.exe"; char strDefaultIcon[MAX_PATH + 1] = "C:\WellTest\trunk\bin\WellTest.exe,0"; char strDescribe[100] = "WellTest Interpretation Files"; RegisterFileRelation(strExt, strAppName, strAppKey, strDefaultIcon, strDescribe); }

     

  • 相关阅读:
    python分析文本文件/json
    python中文件操作
    python异常处理
    socket网络模块
    层模型--固定定位
    层模型--相对定位
    层模型--绝对定位
    什么是层模型?
    浮动模型
    流动模型/a标签换行问题
  • 原文地址:https://www.cnblogs.com/xiaoyusmile/p/3578631.html
Copyright © 2011-2022 走看看