【问题标题】:How to get the status of storage used by an App programmatically?如何以编程方式获取应用程序使用的存储状态?
【发布时间】:2012-04-11 08:27:07
【问题描述】:

我有一个应用程序可以存储图像和文本等用户数据。我想显示我的应用程序消耗了多少磁盘空间。这可能吗?

【问题讨论】:

  • 您将数据存储在哪里?是在 sqlite 文件中还是在 NSDocumentsDirectory 中或使用 NSFileManager

标签: iphone ios data-storage


【解决方案1】:

我不知道这三个,但我已经做了一些事情来通过文件管理器获取目录中文档的大小,这可能会对你有所帮助,这里是代码

[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
        if (isDir) {
            NSPipe *pipe = [NSPipe pipe];
            NSTask *t = [[[NSTask alloc] init] autorelease];
            [t setLaunchPath:@"/usr/bin/du"];
            [t setArguments:[NSArray arrayWithObjects:@"-k", @"-d", @"0", path, nil]];
            [t setStandardOutput:pipe];
            [t setStandardError:[NSPipe pipe]];

            [t launch];

            [t waitUntilExit];

            NSString *sizeString = [[[NSString alloc] initWithData:[[pipe fileHandleForReading] availableData] encoding:NSASCIIStringEncoding] autorelease];
            sizeString = [[sizeString componentsSeparatedByString:@" "] objectAtIndex:0];
            bytes = [sizeString longLongValue]*1024;
        }
        else {
            bytes = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] fileSize];
        }

bytes 会返回你的大小

【讨论】:

    猜你喜欢
    • 2012-03-25
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    相关资源
    最近更新 更多