1

CompactFrameworkでアプリケーションを開発します。WindowsMo​​bile6.x用のnet3.5c#。

アプリケーションをアンインストールする際に、レジスター内のいくつかのキーとその内容を含むフォルダーを削除したいと思います。

インターネットを検索すると、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を呼び出します。

この実装は理にかなっているように見えます (ただし、「IsDots」は CE では省略できます)。

編集
上でリンクされた実装には、実際に 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 に答える