2015-03-25 62 views

回答

0

使用this answer找到一個文件夾的大小

- (unsigned long long int)folderSize:(NSString *)folderPath { 
    NSArray *filesArray = 
    [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath 
                 error:nil]; 
    NSEnumerator *filesEnumerator = [filesArray objectEnumerator]; 
    NSString *fileName; 
    unsigned long long int fileSize = 0; 

    while (fileName = [filesEnumerator nextObject]) { 
    NSDictionary *fileDictionary = 
     [[NSFileManager defaultManager] 
     attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:fileName] 
         error:nil]; 
     fileSize += [fileDictionary fileSize]; 
    } 

    return fileSize; 
} 

調用此

NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; 
NSString *documentsDirectory = 
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
             NSUserDomainMask, 
             YES) objectAtIndex:0]; 
NSString *cachesDirectory = 
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 
             NSUserDomainMask, 
             YES) objectAtIndex:0]; 

unsigned long long int bundleSize = [self folderSize:bundlePath]; 
unsigned long long int docsSize = [self folderSize:documentsDirectory]; 
unsigned long long int cachesSize = [self folderSize:cachesDirectory]; 

long double totalSize = (bundleSize + docsSize + cachesSize)/1024.0/1024.0; 

這會給您的應用程序的包,文件目錄的大小,以兆字節緩存目錄。

相關問題