1

在 Compact Framework 上开发应用程序。NET 3.5 c# 用于 Windows Mobile 6.x。

在卸载应用程序时,想删除注册表中的一些键和包含其内容的文件夹。

在网上搜索了其他关于如何使用SetupDLL Cab项目的提示,发现我必须创建ac++项目,实现方法

Install_Init - 在安装开始之前调用。

Install_Exit - 在安装应用程序后调用。

Uninstall_Init - 在卸载应用程序之前调用。

Uninstall_Exit - 在卸载应用程序后调用。

如链接中所述:http: //www.christec.co.nz/blog/archives/119

好吧,我最大的困难是删除一个文件夹及其所有内容,并使用 C++ 删除寄存器中的一个键。

我尝试了几种在互联网上查找的方法,但都没有编译。

请,现在超过3天我仍然无法解决这个问题。

C++ 中删除递归目录的示例方法:

    bool RemoveDirectory(string path) {
    if (path[path.length()-1] != '\\') path += "\\";
    // first off, we need to create a pointer to a directory
    DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
    pdir = opendir (path.c_str());
    struct dirent *pent = NULL;
    if (pdir == NULL) { // if pdir wasn't initialised correctly
        return false; // return false to say "we couldn't do it"
    } // end if
    char file[256];

    int counter = 1; // use this to skip the first TWO which cause an infinite loop (and eventually, stack overflow)
    while (pent = readdir (pdir)) { // while there is still something in the directory to list
        if (counter > 2) {
            for (int i = 0; i < 256; i++) file[i] = '\0';
            strcat(file, path.c_str());
            if (pent == NULL) { // if pent has not been initialised correctly
                return false; // we couldn't do it
            } // otherwise, it was initialised correctly, so let's delete the file~
            strcat(file, pent->d_name); // concatenate the strings to get the complete path
            if (IsDirectory(file) == true) {
                RemoveDirectory(file);
            } else { // it's a file, we can use remove
                remove(file);
            }
        } counter++;
    }

    // finally, let's clean up
    closedir (pdir); // close the directory
    if (!rmdir(path.c_str())) return false; // delete the directory
    return true;
}

谢谢

4

1 回答 1

2

我在您的代码中看到的最大问题是它都是 ASCII(char、strcat 等),而 CE 使用 Unicode。我也不完全理解您传递诸如“字符串”类型之类的东西-您必须导入一些未显示的东西。

您真正需要的是在路径中调用FindFirstFile,然后迭代调用DeleteFileFindNextFile,直到没有更多文件。文件夹为空后,调用RemoveDirectory

这个实现看起来很合理(尽管 CE 可以省略“IsDots”的东西)。

编辑
原来,上面链接的实现在 Unicode 环境中实际使用时存在错误。我已经修复了它并使其与 CE 兼容(同时保持桌面兼容性)。这是更新的版本:

BOOL DeleteDirectory(TCHAR* sPath) {
   HANDLE hFind;    // file handle
   WIN32_FIND_DATA FindFileData;

   TCHAR DirPath[MAX_PATH];
   TCHAR FileName[MAX_PATH];

   _tcscpy(DirPath, sPath);
   _tcscat(DirPath, _T("\\*"));    // searching all files
   _tcscpy(FileName, sPath);
   _tcscat(FileName, _T("\\"));

   // find the first file
   hFind = FindFirstFile(DirPath,&FindFileData);
   if(hFind == INVALID_HANDLE_VALUE) return FALSE;
   _tcscpy(DirPath,FileName);

   bool bSearch = true;
   while(bSearch) {    // until we find an entry
      if(FindNextFile(hFind,&FindFileData)) {
         _tcscat(FileName,FindFileData.cFileName);
         if((FindFileData.dwFileAttributes &
            FILE_ATTRIBUTE_DIRECTORY)) {

            // we have found a directory, recurse
            if(!DeleteDirectory(FileName)) {
                FindClose(hFind);
                return FALSE;    // directory couldn't be deleted
            }
            // remove the empty directory
            RemoveDirectory(FileName);
             _tcscpy(FileName,DirPath);
         }
         else {
            if(FindFileData.dwFileAttributes &
               FILE_ATTRIBUTE_READONLY)
                SetFileAttributes(FindFileData.cFileName, 
                    FindFileData.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
                  if(!DeleteFile(FileName)) {    // delete the file
                    FindClose(hFind);
                    return FALSE;
               }
               _tcscpy(FileName,DirPath);
         }
      }
      else {
         // no more files there
         if(GetLastError() == ERROR_NO_MORE_FILES)
         bSearch = false;
         else {
            // some error occurred; close the handle and return FALSE
               FindClose(hFind);
               return FALSE;
         }

      }

   }
   FindClose(hFind);                  // close the file handle

   return RemoveDirectory(sPath);     // remove the empty directory
}
于 2011-02-15T14:17:27.057 回答