如何获取iPhone资源文件夹中的文件夹和文件列表?

编程入门 行业动态 更新时间:2024-10-08 08:26:21
本文介绍了如何获取iPhone资源文件夹中的文件夹和文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在我的资源文件夹中进行文件夹结构,例如...资源=> MyData => S1 然后在S1 => Name.png,data.ppt

I am doing folder structure in my resource folder like... Resource => MyData => S1 then in S1=> Name.png, data.ppt

现在我想获取所有文件夹列表和文件名。这里MyData名称只是静态其他可能会改变。就像我可以添加S1,S2,S3和文件数量。那么如何通过Objective C阅读这些内容呢?我试过下面的代码。

Now I want to get all folder list and file names. Here MyData name will be only static other may change.Like I can add S1, S2 , S3 and number of files in that. So how to read these contents through Objective C? I tried below code.

NSString *bundlePathName = [[NSBundle mainBundle] bundlePath]; NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:@"Resources"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:dataPathName]) { NSLog(@"%@ exists", dataPathName); BOOL isDir = NO; [fileManager fileExistsAtPath:dataPathName isDirectory:(&isDir)]; if (isDir == YES) { NSLog(@"%@ is a directory", dataPathName); NSArray *contents; contents = [fileManager contentsOfDirectoryAtPath:dataPathName error:nil]; for (NSString *entity in contents) { NSLog(@"%@ is within", entity); } } else { NSLog(@"%@ is not a directory", dataPathName); } } else { NSLog(@"%@ does not exist", dataPathName); }

谢谢,

推荐答案

您可以像这样获得 Resources 目录的路径,

You can get the path to the Resources directory like this,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];

然后将 Documents 附加到路径中,

Then append the Documents to the path,

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];

然后你可以使用 NSFileManager的任何目录列表API 。

NSError * error; NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

更多推荐

如何获取iPhone资源文件夹中的文件夹和文件列表?

本文发布于:2023-11-28 11:23:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件列表   文件夹   文件   夹中   资源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!