天天看點

VC 删除檔案到資源回收筒

      要删除檔案到資源回收筒,隻要用SHFileOperation函數就行了,下面的代碼我将為你示範了這一個函數的用法。當然你可以直接拷貝到你的項目中。

//删除檔案到資源回收筒中

//pszPath  : 待删除的全路徑檔案名

//bDelete  : TRUE 删除,不移到資源回收筒,FALSE:移到資源回收筒

//傳回    : TRUE 删除成功     FALSE 删除失敗

BOOL CDelFileToRecycleDlg::Recycle(LPCTSTR pszPath, BOOL bDelete)

{

    SHFILEOPSTRUCT  shDelFile;

    memset(&shDelFile,0,sizeof(SHFILEOPSTRUCT));

    shDelFile.fFlags |= FOF_SILENT;      // don't report progress

    shDelFile.fFlags |= FOF_NOERRORUI;     // don't report errors

    shDelFile.fFlags |= FOF_NOCONFIRMATION;    // don't confirm delete

    // Copy pathname to double-NULL-terminated string.

    //

    TCHAR buf[_MAX_PATH + 1]; // allow one more character

    _tcscpy(buf, pszPath);   // copy caller's pathname

    buf[_tcslen(buf)+1]=0;   // need two NULLs at end

    // Set SHFILEOPSTRUCT params for delete operation

    shDelFile.wFunc = FO_DELETE;       // REQUIRED: delete operation

    shDelFile.pFrom = buf;         // REQUIRED: which file(s)

    shDelFile.pTo = NULL;          // MUST be NULL

    if (bDelete)

    {         // if delete requested..

        shDelFile.fFlags &= ~FOF_ALLOWUNDO;    // ..don't use Recycle Bin

    }

    else

    {           // otherwise..

       shDelFile.fFlags |= FOF_ALLOWUNDO;    // ..send to Recycle Bin

    } 

     return SHFileOperation(&shDelFile);    // do it!

}

如果要是直接删除的話用CFile::Remove("D://1.txt");   //删除檔案D盤的1.txt檔案。

ZZ:http://longandrong.blog.sohu.com/19264151.html