文件拷贝(IFileOperation::CopyItem)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值