【问题标题】:Listing files from specific documents directory folder列出特定文档目录文件夹中的文件
【发布时间】:2013-11-29 23:21:35
【问题描述】:

我在文档目录的一个文件夹中保存了许多文件。这是其中之一的路径:

/var/mobile/Applications/120FD973-C902-405E-A645-B1651904CE9B/Documents/1127029/BusinessCard2258009241.card

我想列出文件夹(1127029)中的文件,我试过这个:

    NSFileManager *fManager = [NSFileManager defaultManager];
    NSString *item;
    NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%d", FolderNumber]] error:nil];


    // >>> this section here adds all files with the chosen extension to an array
    for (item in contents){
        if ([[item pathExtension] isEqualToString:@"card"]) {

...

但它不起作用

【问题讨论】:

  • 你调试过你的代码吗?你确认你的路径是正确的吗?分解您的代码,以便您真正看到您正在访问的路径的值。并搜索如何正确访问Documents 文件夹。
  • 兄弟不太确定,但它应该类似于 [NSString stringWithFormat:@"Documents/%d", documentsDirectory]] error:nil];
  • 这里可以参考link
  • 解决了???得到你要找的东西
  • 为什么我被否决了?在我看来这是一个合理的问题......

标签: ios


【解决方案1】:

不要试图以这种方式访问​​文档目录。它行不通。请改用此代码片段:

NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

然后使用 stringByAppendingPathComponent: 添加子目录名称。

【讨论】:

  • 如果我的回答解决了您的问题,我们将不胜感激。 (我不是投票给你的人)
猜你喜欢
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
相关资源
最近更新 更多