gpt4 book ai didi

c++ - CreateFile GetFileTIme 设置文件时间

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:35 27 4
gpt4 key购买 nike

我在使用 GetFileTime 和 SetFileTime 时遇到问题说到目录。具体来说,我认为我的问题是我是 WinAPI 的新手,我不认为我得到了正确处理。

有两种情况。

首先,我只需要一个句柄来获取文件或目录时间戳(创建、访问、修改)。我想以安全灵活的方式制作这个 handle 。不想在参数上过于慷慨。

第二,我需要一个允许我修改文件或目录的句柄时间戳。我还想以最少的权限但以灵活可靠的方式创建此句柄。

我所说的灵活是指在这两种情况下,我都需要代码在本地、网络共享和多线程应用程序中工作。多线程部分不是必需的,因为我的应用程序不会在文件/目录上创建多个句柄,但在后台运行的其他一些应用程序可能会。

//QUESTION 1:
//I do this when I just need a handle to **GET** some attributes like dates.
//(here I just need a handle to get info I am not modding the item).
//Am I using the correct params if I need it to work in a
//local + networked environment and also in a multi-threaded app???
h1 = CreateFile(itemA, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (h1 == INVALID_HANDLE_VALUE){

return 0;
}
//QUESTION 2:
//The above works for local files but not local dirs.
//How can I get the above to work for dirs? (Same environment considerations).


//QUESTION 3:
//I do this when I just need a handle to ***SET*** some attributes (like timestamps).
//(here I need a handle that allows me to modd the items timestamp).
//Am I using the correct params if I need it to work in a
//local + networked environment and also in a multi-threaded app???
hItemB = CreateFile(itemB, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (hItemB == INVALID_HANDLE_VALUE){
return 0;
}
//QUESTION 4:
//The above works for local files but not local dirs.
//How can I get the above to work for dirs? (Same environment considerations).

最佳答案

答案 #2:要使用 CreateFile 获取目录句柄,您需要使用 FILE_FLAG_BACKUP_SEMANTICS 标志。使用您的示例:

h1 = CreateFile(itemA, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);

我猜这也适用于答案 #4,但我还没有尝试确认。

关于c++ - CreateFile GetFileTIme 设置文件时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4998814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com