【发布时间】:2011-06-27 07:05:59
【问题描述】:
我在使用 GetFileTime 和 SetFileTime 时遇到问题 当涉及到目录时。具体来说,我认为我的问题是 我是 WinAPI 的新手,我认为我没有得到 正确处理。
有两种情况。
首先,我只需要一个句柄来获取文件或目录 时间戳(创建、访问、修改)。我想以一种安全灵活的方式制作这个手柄。 参数上不要太大方。
其次,我需要一个允许我修改文件或目录的句柄 时间戳。我还想以最低权限但以灵活可靠的方式创建此句柄。
灵活是指在这两种情况下,我都需要代码在本地、网络共享和多线程应用程序中工作。多线程部分不是必需的,因为我的应用程序不会在文件/目录上创建多个句柄,但在后台运行的其他一些应用程序可能会这样做。
//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).
【问题讨论】: