|
ファイルをゴミ箱に移動する |
| ダウンロード : ゴミ箱移動サンプル(2006/02/26) |
|
#include "stdafx.h" #include "TestTrashBox.h" #include <string.h> #include <shellapi.h> int APIENTRY _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { if (__argc <= 1) return EXIT_SUCCESS; // 全て文字列長さを算出する int nLenAll = 1; // 終端 \0 int i; for (i = 1; i < __argc; i++) nLenAll += (int)_tcslen(__argv[i]) + 1; // ファイル名連結エリアを動的取得する TCHAR* pFile = new TCHAR[nLenAll]; if (!pFile) return EXIT_FAILURE; // 文字列を連結する TCHAR* pAdd = pFile; for (i = 1; i < __argc; i++) { _tcscpy(pAdd, __argv[i]); pAdd += _tcslen(__argv[i]); *pAdd++ = _T('\0'); } *pAdd = _T('\0'); // ゴミ箱にファイルを移動する SHFILEOPSTRUCT stFile = { 0 }; // stFile.hwnd = hWnd; stFile.wFunc = FO_DELETE; stFile.pFrom = pFile; stFile.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; int nResult = ::SHFileOperation(&stFile) ? EXIT_FAILURE : EXIT_SUCCESS; delete[] pFile; return nResult; } |
|
BOOL DeleteFileToTrashBox(TCHAR* pszDeleteFile) { int nLen = (int)_tcslen(pszDeleteFile); TCHAR* pFile = new TCHAR[nLen + 2]; if (!pFile) return FALSE; _tcscpy(pFile, pszDeleteFile); pFile[nLen] = pFile[nLen + 1] = _T('\0'); // ゴミ箱にファイルを移動する SHFILEOPSTRUCT stFile = { 0 }; // stFile.hwnd = hWnd; stFile.wFunc = FO_DELETE; stFile.pFrom = pFile; stFile.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; BOOL bSuccess = BOOL(!::SHFileOperation(&stFile)); delete[] pFile; return bSuccess; } |