首页 > 编程语言 > 详细

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

时间:2017-02-05 17:57:46      阅读:297      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:http://www.cnblogs.com/kabe/p/6368093.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!