如何在Windows上按句柄重命名文件
How to rename a file by handle on Windows?
在Windows上,如何仅使用句柄重命名文件?
我不控制文件的打开方式(它是通过专有的第三方库完成的)。然而,我可以检索这个文件的句柄(见#1)。
我还知道专有库使用以下属性打开文件:
GENERIC_WRITE | GENERIC_READ
和FILE_SHARE_WRITE | FILE_SHARE_READ
。
我已经尝试使用SetFileInformationByHandle
函数与FileRenameInfo
作为参数。不幸的是,这似乎只有在文件以DELETE访问类型打开时才有效,而这里不是这种情况。
你有什么办法可以做到我想要的吗?
提前感谢。
#1:注意库不允许直接访问文件句柄。然而,它给了我文件名和路径。然后使用NtQuerySystemInformation和NtQueryObject函数检索句柄。NtQuerySystemInformation允许我检索当前进程的所有句柄列表(使用SystemInformationClass参数的值16),然后我使用NtQueryObject查找库根据其文件路径打开的确切句柄。所以我没有打开一个单独的句柄。
/* Here is a basic pseudo-code demonstrating what I am trying to achieve */
library::Initialize(); //This creates a new file with a random name. The library keeps a handle opens internally until we call library::close.
file_info_struct tFileInfo;
library::GetFileInfo(tFileInfo); //This gives me information about the created file
HANDLE hFile = my::GetHandleFromFilePath(tFileInfo.file_path); //This function uses NtQuerySystemInformation and NtQueryObject functions to retrieve the existing handle
my::RenameFileByHandle(hFile, someNewFileName); //This is what I am missing. I do not know how to rename the file using its handle
//Carry on with using the library
....
library::close(); //This will close the internal file handle
SetFileInformationByHandle,访问NtSetInformationFile的正确方法,Vista新功能
使用NtSetInformationFile与FileRenameInformation信息类。注意,句柄必须以DELETE权限打开。
使用API调用GetFinalPathNameByHandle获取文件名,然后使用MoveFile API重命名文件
但是我认为你应该在获得文件名后关闭该文件否则移动/重命名操作将失败
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 通过重命名C++中的符号来减小二进制大小
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 如何获取边缘窗口句柄 (HWND)?
- 枚举进程模块在有效句柄上返回无效句柄
- 在 c/c++ 中打开 PhysicalDrive 的句柄并写入 MBR 的 linux 等效是什么?
- 如何以与 API 兼容的方式重命名类成员?
- 从运行服务的应用程序代码中提取窗口句柄
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 在读取文件后重置句柄
- 是否有像地图这样的C++结构,但我得到的不是值的键,而是值的句柄?
- 在Qt中复制,剪切,粘贴,重命名,撤消和重做
- Microsoft ODBC 无法创建有效的句柄
- 重命名 win32api 函数以进行混淆
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 将文件句柄重定向到字符缓冲区
- 比较两个命名的管道句柄
- 如何在Windows上按句柄重命名文件
- 是否可以将cout流重定向到具有hwnd句柄的特定窗口(控件)