Winapi 使用SetFileInformationByHandle重命名文件,但会将其删除
我正在使用Winapi 使用SetFileInformationByHandle重命名文件,但会将其删除,winapi,Winapi,我正在使用SetFileInformationByHandle()函数重命名文件 我用ReplaceIfExists=TRUE(文件重命名信息结构的成员)传入了FileRenameInfo,但它没有重命名文件,而是删除了文件。我没有看到任何重命名的文件,也没有从函数调用中得到任何错误。我建议您检查中的参数设置是否正确。不正确的设置可能导致重命名过程被意外删除 以下是使用重命名文件的示例: #include <windows.h> #include <iostream>
SetFileInformationByHandle()
函数重命名文件
我用
ReplaceIfExists=TRUE
(文件重命名信息结构的成员)传入了FileRenameInfo
,但它没有重命名文件,而是删除了文件。我没有看到任何重命名的文件,也没有从函数调用中得到任何错误。我建议您检查中的参数设置是否正确。不正确的设置可能导致重命名过程被意外删除
以下是使用重命名文件的示例:
#include <windows.h>
#include <iostream>
int main()
{
auto const& filepath = L"D:\\test\\file.txt";
auto const& destpath = L"D:\\test\\other.txt";
auto const f_handle = CreateFile(filepath,
GENERIC_READ | GENERIC_WRITE | DELETE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (f_handle == INVALID_HANDLE_VALUE)
{
auto const err = GetLastError();
std::cerr << "failed to open: " << err;
return err;
}
auto const destpath_bytes_with_null = sizeof(destpath);
auto const struct_size = sizeof(FILE_RENAME_INFO) + destpath_bytes_with_null;
FILE_RENAME_INFO* fri = (FILE_RENAME_INFO*)new BYTE[struct_size];
fri->ReplaceIfExists = TRUE;
fri->FileNameLength = destpath_bytes_with_null;
fri->RootDirectory = NULL;
std::memcpy(fri->FileName, destpath, destpath_bytes_with_null);
BOOL res = SetFileInformationByHandle(f_handle, FileRenameInfo,
fri, struct_size);
if (!res)
{
auto const err = GetLastError();
std::cerr << "failed to rename file: " << err;
return err;
}
else
std::cout << "success";
}
#包括
#包括
int main()
{
auto const&filepath=L“D:\\test\\file.txt”;
自动常量&destpath=L“D:\\test\\other.txt”;
auto const f_handle=CreateFile(文件路径,
一般读取、一般写入、删除、,
0,
无效的
开放式,
文件\u属性\u正常,
无效);
if(f_handle==无效的_handle_值)
{
auto const err=GetLastError();
std::cerr FileNameLength=destpath\u bytes\u,带null;
fri->RootDirectory=NULL;
std::memcpy(fri->FileName,destpath,destpath\u字节,带\u null);
BOOL res=SetFileInformationByHandle(f_handle,FileRenameInfo,
fri,结构尺寸);
如果(!res)
{
auto const err=GetLastError();
请出示您的实际代码嗨,如果这个答案对您有帮助,请随时标记它以帮助有相同问题的人,如果您有任何问题,请告诉我。