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

本文提供两个实用的C++代码示例:一是递归删除指定目录及其下所有文件;二是让应用程序自行删除自身。前者适用于清理不再需要的整个目录结构,后者则在特定情况下(如卸载程序)非常有用。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值