SHFileOperation()函数主要对文件夹的四种操作:复制,删除,移动,重命名

//SHFileOperations的操作,pathFrom接受变量传值时老是出错,手工在字符串后面加2个'\0',就可以了,如果是直接手写路径,微软库自动作此处理了
bool gAppSHFileOperation(const QString& pathFrom, const QString& pathTo, const QString& title, const int flags)
{
    // flags为传入的操作:复制,删除,移动,重命名

    TCHAR wcFrom[MAX_PATH+1]    = {0};
    TCHAR wcTo[MAX_PATH+1]      = {0};
    TCHAR wcTitle[MAX_PATH+1]   = {0};

    wcsncpy_s(wcFrom, (TCHAR*)pathFrom.utf16(), pathFrom.length());
    wcsncpy_s(wcTo, (TCHAR*)pathTo.utf16(), pathTo.length());
    wcsncpy_s(wcTitle, (TCHAR*)title.utf16(), (title.length() > MAX_PATH ? 0 : title.length()));

    wcFrom[pathFrom.length()] = '\0';
    wcFrom[pathFrom.length()+1] = '\0';
    wcTo[pathTo.length()] = '\0';
    wcTo[pathTo.length()+1] = '\0';
    wcTitle[(title.length() > MAX_PATH ? 0 : title.length())] = '\0';
    wcTitle[(title.length() > MAX_PATH ? 0 : title.length())+1] = '\0';


    SHFILEOPSTRUCT lpFileOp = {0};
    lpFileOp.hwnd  =    NULL;
    lpFileOp.wFunc  =    flags; //FO_DELETE
    lpFileOp.pFrom   =    wcFrom;
    lpFileOp.pTo     =    wcTo;
    lpFileOp.fFlags  =    FOF_NOERRORUI|FOF_NOCONFIRMATION|FOF_SILENT; //不提示覆盖,不显示错误,不显示进度
    lpFileOp.lpszProgressTitle = wcTitle;

    return  (SHFileOperation(&lpFileOp)== 0);
}

 

posted @ 2014-10-18 15:05  纠纠结结  阅读(543)  评论(0编辑  收藏  举报