紫水晶编程技术论坛 - 努力打造成全国最好的编程论坛

 找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4270|回复: 2

[开源] 递归删除FOLDER和注册表KEY(纯C语言WIN32API单函数无注释版)

[复制链接]

854

主题

3481

帖子

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

Rank: 125Rank: 125Rank: 125Rank: 125Rank: 125

积分
36100
发表于 2014-4-8 17:43:19 | 显示全部楼层 |阅读模式
删文件夹:
  1. void DeleteFolder(char *szFilePath)
  2. {
  3.     WIN32_FIND_DATAA fd;
  4.     char szSearch[MAX_PATH]={0};
  5.     strcpy(szSearch,szFilePath);
  6.     strcat(szSearch,"*.*");
  7.     HANDLE h=FindFirstFileA(szSearch,&fd);
  8.     if(h!=INVALID_HANDLE_VALUE)
  9.     {
  10.         BOOL bRet=TRUE;
  11.         while(bRet)
  12.         {
  13.             bRet=FindNextFileA(h,&fd);
  14.             if(stricmp(fd.cFileName,".."))
  15.             {
  16.                 char tmp[MAX_PATH]={0};
  17.                 strcpy(tmp,szFilePath);
  18.                 strcat(tmp,fd.cFileName);
  19.                 if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  20.                 {
  21.                     strcat(tmp,"\");
  22.                     //printf("[DIR]%s\n",tmp);
  23.                     DeleteFolder(tmp);
  24.                     RemoveDirectoryA(tmp);
  25.                 }
  26.                 else
  27.                 {
  28.                     //printf("[FIL]%s\n",tmp);
  29.                     DeleteFileA(tmp);
  30.                 }
  31.             }
  32.         }
  33.         FindClose(h);
  34.     }
  35.     RemoveDirectoryA(szFilePath);
  36. }
复制代码

删注册表键:
  1. VOID RegDeleteKeys(HKEY RootKey, const char *pSubKey)
  2. {
  3.     HKEY hKey;
  4.     DWORD nRet,NameCnt,NameMaxLen,KeyCnt,KeyMaxLen,MaxDateLen;
  5.     nRet=RegOpenKeyExA(RootKey,pSubKey,0,KEY_ALL_ACCESS,&hKey);
  6.     if(nRet!=ERROR_SUCCESS)
  7.     {
  8.         return;
  9.     }
  10.     nRet = RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCnt,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL);
  11.     if(nRet == ERROR_SUCCESS)
  12.     {
  13.         for(int dwIndex = KeyCnt - 1; dwIndex >= 0; dwIndex--)
  14.         {
  15.             char sKeyName[256] = {0}, pSubKeyTemp[256] = {0};
  16.             RegEnumKeyA(hKey, dwIndex, sKeyName, sizeof(sKeyName));
  17.             HKEY hKeySub;
  18.             DWORD KeyCntSub;
  19.             strcpy(pSubKeyTemp, pSubKey);
  20.             strcat(pSubKeyTemp, "\");
  21.             strcat(pSubKeyTemp, sKeyName);
  22.             nRet = RegOpenKeyExA(RootKey,pSubKeyTemp,0,KEY_ALL_ACCESS,&hKeySub);
  23.             if(nRet == ERROR_SUCCESS)
  24.             {
  25.                 nRet = RegQueryInfoKey(hKeySub,NULL,NULL,NULL,&KeyCntSub,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL);
  26.                 if(nRet == ERROR_SUCCESS)
  27.                 {
  28.                     if (KeyCntSub != 0)
  29.                     {
  30.                         RegDeleteKeys(RootKey, pSubKeyTemp);
  31.                     }
  32.                     RegCloseKey(hKeySub);
  33.                 }
  34.             }
  35.             RegDeleteKeyA(RootKey ,pSubKeyTemp);
  36.         }
  37.         RegCloseKey(hKey);
  38.     }
  39.     RegDeleteKeyA(RootKey ,pSubKey);
  40. }
复制代码

例子:
  1. DeleteFolder("c:\\windows\");
  2. RegDeleteKeys(HKEY_LOCAL_MACHINE,"SYSTEM\\ControlSet001\\services");
复制代码

30

主题

723

帖子

0

精华

钻石会员

Rank: 6Rank: 6

积分
2815
发表于 2015-4-23 19:46:04 | 显示全部楼层
谢谢分享 {:soso_e195:}

0

主题

52

帖子

0

精华

钻石会员

Rank: 6Rank: 6

积分
3863
发表于 2019-5-7 09:50:53 | 显示全部楼层
谢谢
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

手机版|Archiver|紫水晶工作室 ( 粤ICP备05020336号 )

GMT+8, 2024-4-19 00:13 , Processed in 0.023383 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表