gpt4 book ai didi

VC++实现添加文件关联的方法示例

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章VC++实现添加文件关联的方法示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了VC++实现添加文件关联的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 检测文件关联情况
// 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);
}

用法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CguanlianDlg::OnBnClickedOk()
{
   // TODO: 在此添加控件通知处理程序代码
   //使用这两个函数进行关联的示例代码
   char strExt[10] = ".zwcTxt" ;
   char strAppKey[30] = "Windows.zwcTxt.1.0" ;
   BOOL relationExists = CheckFileRelation(strExt, strAppKey);
   if (!relationExists)
   {
     char strAppName[MAX_PATH + 1] = "E:\\Desktop\\Temp\\GMTools.exe" ;
     char strDefaultIcon[MAX_PATH + 1] = "" ;
     char strDescribe[100] = "WellTest Interpretation Files" ;
     RegisterFileRelation(strExt, strAppName, strAppKey, strDefaultIcon, strDescribe);
   }
}

希望本文所述对大家VC++程序设计有所帮助.

原文链接:http://blog.csdn.net/zwc2xm/article/details/55254895 。

最后此篇关于VC++实现添加文件关联的方法示例的文章就讲到这里了,如果你想了解更多关于VC++实现添加文件关联的方法示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com