1

我有一个函数可以检查 /User/Library/Preferences/ 目录中几个 plist 文件的大小。出于测试目的,我使用的是 iTunes,它在我的机器上有一个 ~500kb 的首选项文件。

编辑:我已根据答案更正了我的代码 - 如发布的那样,此代码工作正常。

    NSString *obj = @"iTunes";
    NSString *filePath = [NSString stringWithFormat:@"/Applications/%@.app",obj];
    NSString *bundle = [[NSBundle bundleWithPath:filePath] bundleIdentifier];

    NSString *PropertyList=[NSString stringWithFormat:@"/Preferences/%@.plist",bundle];
    NSString* fileLibraryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:PropertyList];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileLibraryPath];

    if (fileExists) {
        NSError *err = nil;
        NSDictionary *fattrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileLibraryPath error:&err];

        if (fattrib != nil){            
            //Here I perform my comparisons

            NSLog(@"%i %@", [fattrib fileSize],obj);

        }
    }

但是,无论我做什么,返回的大小都是 102。不是 102kb,只是 102。我使用了 objectForKey:NSFileSize,我使用了 stringValue,都是 102。


正如下面所选答案中所述,经验教训是始终检查您提交给 NSFileManager 的路径。

谢谢!

4

1 回答 1

3

filePath您正在使用的

NSDictionary *fattrib = [ ... attributesOfItemAtPath:filePath error:&err];

似乎

/Applications/iTunes.app

在我的系统上是一个大小为 102 字节的目录,与/Applications/Mail.app- 102 字节相同。只是路径不是你想要的吗?

于 2011-01-19T21:34:39.670 回答