-1

作为标题,使用 NSFileManager 我可以获得文件创建时间和修改时间,而我需要的是最后更改时间。任何想法将不胜感激。

4

3 回答 3

2

在 cocoa 中做这类事情的首选方法是使用 NSURL。

您可能想使用 fileURLWithPath:isDirectory: 或 fileURLWithPath: 创建 NSURL 实例,然后使用 getResourceValue:forKey:error: 或 resourceValuesForKeys:error: 方法来获取您的资源值。

用于时间戳的资源键是 NSURLAttributeModificationDateKey、NSURLContentAccessDateKey、NSURLContentModificationDateKey 和 NSURLCreationDateKey。

于 2012-08-09T05:40:57.667 回答
1

既然您知道最后状态更改时间之类的东西,为什么不直接使用 stat(2) 呢?还是我误解了这个问题?

stat(2) 会一直给你;创建、修改、访问和元数据已更改;它不是高级别的,但调用起来很简单。

于 2012-08-08T21:15:50.007 回答
0

最后我使用以下方法。

#include <sys/stat.h>

long lastChangedTimestamp = 0;
struct stat fileStatus;
if(0 == stat(filename, &fileStatus))
{
    lastChangedTimestamp = fileStatus.st_ctimespec.tv_sec;
}
于 2012-08-09T09:54:45.250 回答