博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件拷贝(IFileOperation::CopyItem)
阅读量:4297 次
发布时间:2019-05-27

本文共 3726 字,大约阅读时间需要 12 分钟。

UINT CFileCopyView::CopyThread(){    int nCount = m_SourceListCtrl.GetCheckCount();    if (nCount == 0)    {        return 0;    }    // 2:Begin    m_progressDlgExt.Start();    HRESULT hr = S_OK;    LVITEM lvItem = { 0 };    CString strFilePath = _T("");    LPAFX_SHELLITEMINFO2 pItem = NULL;    int nResult = COPY_Status_OK;    int nFileOK = 0;    ::CoInitialize(NULL);    // 2.1:リリース    for (int nIndex = 0; nIndex < m_SourceListCtrl.GetItemCount(); nIndex++)    {        if (m_SourceListCtrl.GetCheck(nIndex) == FALSE)            continue;        // init        memset(&lvItem, 0x00, sizeof(LVITEM));        strFilePath = _T("");        pItem = NULL;        // GetItem        lvItem.iItem = nIndex;        lvItem.mask = LVIF_PARAM;        m_SourceListCtrl.GetItem(&lvItem);        pItem = (LPAFX_SHELLITEMINFO2)lvItem.lParam;        if (pItem == NULL)        continue;        strFilePath = SHGetFilePath(pItem->pParentFolder, pItem->pidlRel);        if (strFilePath.IsEmpty())        continue;        // Copy        CString strDestPath = m_strTargetPath + pItem->szFQPath;        IFileOperation* fileoperation = NULL;        hr = ::CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_IFileOperation, (void**)&fileoperation);        if (hr == S_OK && fileoperation)        {            if (COPYFILE)                fileoperation->SetOperationFlags(FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR);            else                fileoperation->SetOperationFlags(FOF_NO_UI);            fileoperation->SetOwnerWindow(NULL);            // From            IShellItem *pItemFrom = NULL;            IShellItem *pItemTo = NULL;            BOOL aborted = false;            // 1.From            hr = SHCreateItemFromParsingName(strFilePath, NULL, IID_IShellItem, (void**)(&pItemFrom));            if (hr == S_OK && pItemFrom)            {                // 2.To                hr = SHCreateItemFromParsingName(strDestPath, NULL, IID_IShellItem, (void**)(&pItemTo));                // Check Folder                if (hr != S_OK || pItemTo == NULL)  // Folder not exiset                {                    hr = SHCreateDirectory(NULL, strDestPath);                    if (hr == S_OK)                    {                        hr = SHCreateItemFromParsingName(strDestPath, NULL, IID_IShellItem, (void**)(&pItemTo));                    }                }                if (hr == S_OK && pItemTo)                {                    // 3.Copy                    hr = fileoperation->CopyItem(pItemFrom, pItemTo, NULL, NULL);                    fileoperation->PerformOperations();                    fileoperation->GetAnyOperationsAborted(&aborted);                    if (aborted)                    {                        nResult = COPY_Status_Aborted;                    }                    else                    {                        nFileOK++;                    }                    pItemTo->Release();                }                else                {                    nResult = COPY_Status_Error;                }                pItemFrom->Release();            }            else            {                nResult = COPY_Status_Error;            }            fileoperation->Release();        }        else        {            nResult = COPY_Status_Error;        }    }    ::CoUninitialize();    m_progressDlgExt.Stop();    m_hThread = NULL;    CString strMessage = _T("");    if (nResult == COPY_Status_OK)    {        strMessage.Format(DllLoadIDResource(IDS_MSGInfo_CopyFiles1), nCount, nFileOK);          }    else    {        strMessage.Format(DllLoadIDResource(IDS_MSGInfo_CopyFiles2), nCount, nFileOK, nCount - nFileOK);    }    MessageBox(strMessage, DllLoadIDResource(IDS_MSGTitle_Release));    return 0;}

转载地址:http://hnsws.baihongyu.com/

你可能感兴趣的文章
Docker面试题(一)
查看>>
第四轮面试题
查看>>
第一轮面试题
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
一、redis面试题及答案
查看>>
消息队列2
查看>>
消息列队3
查看>>
spring cloud 面试题总结
查看>>
第二轮面试题
查看>>
2021-04-27
查看>>
SSM 写出乐淘商城
查看>>
高精尖面试题汇总
查看>>
Linux面试题
查看>>
关于NSAutoReleasePool的理解
查看>>
微服务架构系列主题:微服务架构概述
查看>>
微服务架构系列主题:微服务架构解析与实践
查看>>
微服务架构系列主题:微服务架构及其最重要的 10 个设计模式!
查看>>
JavaWeb学习-注解-1-注解快速入门
查看>>
JavaWeb学习-注解-2-注解作用目标限定和保留策略
查看>>