1.将文件放入回收站
void __fastcall TForm1::Button9Click(TObject *Sender)
{
UnicodeString uniFilename = this->Edit1->Text;
if (FileExists(uniFilename, true)) { //判断文件是否存在
const int pathSize = uniFilename.Length() + 2;
wchar_t* from = new wchar_t[pathSize];
wcscpy(from, uniFilename.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; //释放内存
if (result) {
ShowMessage("Del File ok!");
}
}
}
2.将文件夹放入回收站(不论是否为空)
void __fastcall TForm1::Button10Click(TObject *Sender)
{
UnicodeString uniSDir = this->Edit1->Text;//获取需要放入回收站的文件夹
const int pathSize = uniSDir.Length() + 2;
wchar_t* buff = new wchar_t[pathSize]; /
wcscpy(buff, uniSDir.c_str());
buff[pathSize-1] = 0;
SHFILEOPSTRUCT fs;
ZeroMemory(&fs, sizeof(SHFILEOPSTRUCT));
fs.hwnd = Application->Handle;
fs.wFunc = FO_DELETe;
fs.pFrom = buff;
fs.pTo = NULL;
fs.fFlags=FOF_SIMPLEPROGRESS|FOF_NOCONFIRMATION;
bool result = (SHFileOperation(&fs) == 0);
delete[] buff;
if (result) {
ShowMessage("Del File ok!");
}
}
void __fastcall TForm1::Button5Click(TObject *Sender)
{
UnicodeString uniSDir = this->Edit1->Text;//获取需要放入回收站的文件夹(不一定为空)
const int pathSize = uniSDir.Length() + 2;
wchar_t* from = new wchar_t[pathSize];
wcscpy(from, uniSDir.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);//需要用户确认,涉及权限控制
if (result) {
ShowMessage("Del file ok:" + uniSDir);
}
delete[] from;
}
3.清空回收站
void __fastcall TForm1::Button6Click(TObject *Sender)
{
DWORD dwFlags = SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND;
bool result = SHEmptyRecycleBin(Application->Handle, NULL, dwFlags);
if (result) {
ShowMessage("Empty Recycle BIN ok!");
}
}
4.获取系统临时目录
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// C++Builder 11.0 support
UnicodeString uniTmpPath;
uniTmpPath = TPath::GetTempPath();//获取系统临时目录,需要#include
if (uniTmpPath == "")//若获取不到,抛出异常
{
throw Exception("Cannot get temporary direcotry.");
}
this->Edit1->Text = uniTmpPath;
}
5.生成唯一的临时文件名
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// C++Builder 11.0 support
UnicodeString uniTmpFilename;
uniTmpFilename = TPath::GetTempFileName(); //生成唯一的临时文件名,结果返回创建的文件名,需要#include
if (uniTmpFilename == "") //一般权限不够会失败
{
throw Exception("Cannot get temporary filename.");
}
this->Edit1->Text = uniTmpFilename;
}