织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>PC常识>

如何实现文件关联

jackyhwei 发布于 2011-10-13 09:53 点击:次 
做了安装包,但是程序没办法把一些相应的文件关联起来。所以还是得从安装里来实现关联。
TAG:

文件关联,网上资料不多,提供了两个简单的函数,使用方便。
#include <windows.h>
#include <stdio.h>

//---------------------------------------------------------------------------
// 检测文件关联情况
// 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:/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);
}

(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201110/15127.html]
本文出处:网络 作者:秩名
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容