使用文件管理器创建文件副本不适用于mac app

时间:2011-04-20 04:18:32

标签: cocoa nsfilemanager

我的文档文件夹中的某个文件夹中有一个文件。我想在其他文件夹中创建该文件的副本,其他名称。我正在使用的代码是

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:
    [NSString stringWithFormat:@"Documents/test/test1/%@%@%@",str,currentaudioTobPlayed,@".mp3"]];
NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
    [NSString stringWithFormat:@"Documents/test/test1/"]];

NSLog(@"filePat %@",filePath);
NSLog(@"filePath2 %@",filePath2);
NSLog(@"filePath2 %@",fileManager);

[fileManager copyItemAtPath:filePath toPath:filePath2 error:NULL];
[fileManager release];

但是这段代码没有用。请帮忙!

1 个答案:

答案 0 :(得分:0)

在:

NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
    [NSString stringWithFormat:@"Documents/test/test1/"]];

您没有指定目标文件名:〜/ Documents / test / test1是一个目录。

你可以这样做:

NSString *sourceName = [NSString stringWithFormat:@"%@%@.mp3",
    str, currentaudioTobPlayed];

// Choose the destination file name
NSString *destName = [NSString stringWithFormat:@"Copy of %@", sourceName];

NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:@"Documents/test/test1/%@", sourceName]];

NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
    [NSString stringWithFormat:@"Documents/test/test1/%@", destName]];
// Replace Documents/test/test1 with another directory under home,
// or replace the whole string with an entirely different directory