安装包设计-------卸载(MFC)---------知识总结

1、删除目录及其下所有文件

bool MyDeleteFile(CString Path) 
{                                                     
//     SHFILEOPSTRUCT FileOp={0}; 
//     FileOp.fFlags = FOF_ALLOWUNDO | //允许放回回收站
//         FOF_NOCONFIRMATION; //不出现确认对话框
//     FileOp.pFrom = Path; 
//     FileOp.pTo = NULL; //一定要是NULL
//     FileOp.wFunc = FO_DELETE; //删除操作
//     return SHFileOperation(&FileOp) == 0; 



        char* sDirName = new char[Path.GetLength()+1];
        strncpy(sDirName,Path,Path.GetLength()+1);

        CFileFind tempFind; 
        char sTempFileFind[200] ;
        sprintf(sTempFileFind,"%s/*.*",sDirName);
        BOOL IsFinded = tempFind.FindFile(sTempFileFind);
        while (IsFinded)
        { 

            IsFinded = tempFind.FindNextFile();

            if (!tempFind.IsDots())
            { 
                char sFoundFileName[200];
                strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));

                if (tempFind.IsDirectory()) 
                { 
                    CString sTempDir;
                    sTempDir.Format("%s/%s",sDirName,sFoundFileName);
                    MyDeleteFile(sTempDir); 
                }
                else
                { 
                    char sTempFileName[200];
                    sprintf(sTempFileName,"%s/%s",sDirName,sFoundFileName);
                    DeleteFile(sTempFileName); 
                }
            }
        }
        tempFind.Close();
        if(!RemoveDirectory(sDirName)) 
        { 
            return false;
        }
        return true;







}
View Code

2、删除应用程序自身

BOOL DeleteApplicationSelf()

{
    TCHAR tcsExename[MAX_PATH];
    TCHAR tcsParam[MAX_PATH * 2]; 
    TCHAR tcsCmd[MAX_PATH];
    HANDLE hProcess = NULL; // get exe filename and command shell program 
    //_tcscpy(tcsExename,m_appPath);
    if(0 == GetModuleFileName(NULL,tcsExename,MAX_PATH)||0 == GetEnvironmentVariable(_T("COMSPEC"), tcsCmd, MAX_PATH))
    {
        return FALSE;
    } // get short filename for command shell program 
    if( 0 == GetShortPathName(tcsExename, tcsExename, MAX_PATH)) 
    {
        return FALSE;
    } // create a command process, set its priority, then start it.
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si); 
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    ZeroMemory( &pi, sizeof(pi) );
    _stprintf(tcsParam, _T("%s /c del %s"), tcsCmd, tcsExename);
    if(!CreateProcess(NULL,tcsParam,NULL,NULL,FALSE,CREATE_SUSPENDED, NULL,NULL,&si,&pi)) 
    { 
        return FALSE;
        // GetLastError();
    } // heigthen priority of the current process 
    SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); // set file attribute to normal 
    SetFileAttributes(tcsExename, FILE_ATTRIBUTE_NORMAL); // depress priority of command process, then start it 
    SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS); 
    ResumeThread(pi.hThread); 
    return TRUE;
}
View Code
原文地址:https://www.cnblogs.com/kabe/p/6368093.html