Win32APIの SHFileOperation を使用すると、ディレクトリ(フォルダ)をゴミ箱に移動できます。
プロジェクトオプションの「_TCHARのマップ先」を「wchar_t」に設定しています。
/**
* ディレクトリをゴミ箱に移動する
* @param Dir ディレクトリ名
* @return 成功したときはtrue
*/
bool DeleteDir(UnicodeString Dir)
{
//\で終わっていると削除できない
UnicodeString targetDir = ExcludeTrailingPathDelimiter(Dir);
//終端文字は\0\0
const int pathSize = targetDir.Length() + 2;
wchar_t* from = new wchar_t[pathSize];
wcscpy(from, targetDir.c_str());
from[pathSize-1] = 0;
SHFILEOPSTRUCT fs;
ZeroMemory(&fs, sizeof(SHFILEOPSTRUCT));
fs.hwnd = Application->Handle;
fs.wFunc = FO_DELETE;
fs.pFrom = from;
fs.pTo = NULL;
fs.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO;
bool result = (SHFileOperation(&fs) == 0);
delete[] from;
return result;
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
bool result = DeleteDir(L"C:\test\");
/**
* ディレクトリをゴミ箱に移動する
* @param Dir ディレクトリ名
* @return 成功したときはtrue
*/
bool DeleteDir(AnsiString Dir)
{
//\で終わっていると削除できない
Dir = ExcludeTrailingPathDelimiter(Dir);
//終端文字は\0\0
char* from = new char[Dir.Length() + 2];
strcpy(from, Dir.c_str());
from[strlen(from) + 1] = 0;
SHFILEOPSTRUCT fs;
ZeroMemory(&fs, sizeof(SHFILEOPSTRUCT));
fs.hwnd = Application->Handle;
fs.wFunc = FO_DELETE;
fs.pFrom = from;
fs.pTo = NULL;
fs.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO;
bool result = (SHFileOperation(&fs) == 0);
delete[] from;
return result;
}