CreateFile GetFileTime SetFileTime [英] CreateFile GetFileTIme SetFileTime

查看:19
本文介绍了CreateFile GetFileTime SetFileTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble using GetFileTime and SetFileTime when it comes to directories. Specifically I think my problem is that I am new to the WinAPI and I don't think I'm getting the HANDLE correctly.

有两种情况.

首先,我只需要一个句柄来获取文件或目录时间戳(创建、访问、修改).我想以一种安全灵活的方式制作这个手柄.参数不要太大方.

In the first, I just need a handle to get the file or directory timestamps (create,access,mod). I'd like to make this handle in a safe and flexible way. Don't want to be too generous in the parameters.

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

In the second, I need a handle that will allow me to modify the file or direcotry timestamps. I'd also like to create this handle with the minimum rights but in flexible and dependable way.

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

By flexible I mean that in both scenarios I need the code to work both localy, in a network share, and in a multi-threaded app. The multi-threaded part is not necessary because my app will not make multiple handles on the file/dir but it is possible that some other app running in the background will.

//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标志.使用您的示例:

Answer #2: To use CreateFile to get a handle to a directory, you need to use the FILE_FLAG_BACKUP_SEMANTICS flag. Using your example:

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

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

I would guess this would work for Answer #4 as well, but I haven't tried it to confirm.

这篇关于CreateFile GetFileTime SetFileTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆