1、调用示例
cpp
// 调用示例
// 文件重命名
std::wstring strOldFileName = L"E:\\test.json";
std::wstring strNewFileName = L"E:\\testNew.json";
BOOL bReplaceIfExists = FALSE;
BOOL ret = MyFileUtil::ReNameFile(strOldFileName, strNewFileName, bReplaceIfExists);
std::cout << "ret:" << ret <<std::endl;
2、源码
cpp
BOOL MyFileUtil::ReNameFile(const std::wstring& strOldFileName, const std::wstring& strNewFileName, BOOL bReplaceIfExists)
{
if (strOldFileName.empty() || strNewFileName.empty())
{
return FALSE;
}
DWORD dwFlags = MOVEFILE_COPY_ALLOWED;
if (bReplaceIfExists)
{
dwFlags |= MOVEFILE_REPLACE_EXISTING;
}
return ::MoveFileExW(strOldFileName.c_str(), strNewFileName.c_str(), dwFlags);
}