【问题标题】:SetupDLL to remove a folder and a key registerSetupDLL 删除文件夹和密钥寄存器
【发布时间】:2011-06-27 15:18:18
【问题描述】:

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

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

在网上搜索了其他关于如何使用SetupDLL Cab项目的技巧,发现我必须创建一个c++项目,实现方法

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;
}

谢谢

【问题讨论】:

    标签: .net-3.5 compact-framework setup-project cab windows-mobile-6


    【解决方案1】:

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

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

    This implementation 看起来很合理(尽管 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
    }
    

    【讨论】:

    • 当我复制粘贴代码时,出现以下错误: Error 10 error C2664: 'wcscat' : cannot convert parameter 2 from 'const char [3]' to ' const wchar_t *' 错误 11 错误 C2664: 'wcscat' : 无法将参数 2 从 'const char [2]' 转换为 'const wchar_t *' 错误 12 错误 C3861: 'IsDots': 找不到标识符 错误 13 错误 C2065: 'S_IWRITE' : undeclared identifier Error 14 error C3861: '_chmod': identifier not found _拜托,我对 c++ 一无所知,
    • 非常感谢!现在解决我的问题是编译,我测试它是否会删除文件夹。但我相信它会奏效。现在我们需要从寄存器中删除一个键
    • 如何调用这个函数?我正在尝试使用命令:DeleteDirectory ("\Temp\Test");但它给出了错误:警告C4129:'T':无法识别的字符转义序列警告C4129:'T':无法识别的字符转义序列错误C3861:'deleteDirectory':找不到标识符
    • 删除目录(_T("\\temp\\test"));
    • 很抱歉给您添麻烦了,但是编译器没有找到该函数。
    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多