我的文件夹只包含一个子文件夹,我不知道子文件夹的名称。这个子文件夹包含一个html文件,我再也不知道html文件的名称。
我的问题是如何使用
获取此文件的完整路径NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:filename]; //- access sub-folder? //- access html file?
编辑:
我编写了一个返回唯一一个子文件夹的方法,如下所示:
+ (NSString*) get1stSubFolder:(NSString*)folder { NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:folder]; //- no recursive [directoryEnumerator skipDescendents]; NSString* file; while (file = [directoryEnumerator nextObject]) { BOOL isDirectory = NO; BOOL subFileExists = [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory]; if (subFileExists && !isDirectory) { return file; } } return nil; }
我的结果总是为零。你知道我错在哪里吗?
答案 0 :(得分:1)
使用NSDirectoryEnumerator。它应该对你有帮助。
答案 1 :(得分:1)
像这样使用NSDirectoryEnumerator。
NSURL *documentsDirectoryURL = [NSURL URLWithString:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] ];
///If the folder you want to browse for subfolders is NSDocumentDirectory.
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
NSDirectoryEnumerator *enumerator = [[[NSFileManager alloc] init]
enumeratorAtURL:documentsDirectoryURL
includingPropertiesForKeys:keys
options:0
errorHandler:^(NSURL *url, NSError *error) {
return YES;
}];
for (NSURL *url in enumerator) {
NSError *error;
NSNumber *isDirectory = nil;
if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
NSLog(@"Error %@",error);
}
else if ([isDirectory boolValue]) {
NSLog(@"Folder URL: %@",url);
}else{
NSLog(@"File URL: %@",url);
}
}