How to get the file size given a path?

后端 未结 10 1196
自闭症患者
自闭症患者 2020-11-30 20:22

I have a path to file contained in an NSString. Is there a method to get its file size?

10条回答
  •  星月不相逢
    2020-11-30 20:50

    Bear in mind that fileAttributesAtPath:traverseLink: is deprecated as of Mac OS X v10.5. Use attributesOfItemAtPath:error: instead, described at the same URL thesamet mentions.

    With the caveat that I'm an Objective-C newbie, and I'm ignoring errors that might occur in calling attributesOfItemAtPath:error:, you can do the following:

    NSString *yourPath = @"Whatever.txt";
    NSFileManager *man = [NSFileManager defaultManager];
    NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
    UInt32 result = [attrs fileSize];
    

提交回复
热议问题