一、首先头文件的包括
#include "Shellapi.h"
#include "tchar.h"
#pragma comment(lib,"shell32.lib")
二、
字符的转换
std::string 类型转换成 LPCWSTR 类型
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(orig.length() - 1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
三、函数的引用
/拷贝一个文件 和 拷贝一个文件夹基本一致 区别在于路径 一个是文件路径一个是文件夹路径
void _copeFile(std::string & strPath,std::string & desPath)
{
SHFILEOPSTRUCT fop;
fop.wFunc = FO_COPY;
//选择执行类型,FO_COPY,FO_DELETE,FO_RENAME,FO_MOVE四种操作
fop.pFrom = stringToLPCWSTR(strPath);
//源文件来源
fop.pTo = stringToLPCWSTR(desPath);
//目的文件
fop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR;
SHFileOperation(&fop);
}