使用文件管理器创建文件副本不适用于 Mac 应用程序

发布于 2024-11-02 16:50:11 字数 665 浏览 1 评论 0原文

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

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];

但是此代码不起作用。请帮忙!

I have a file in one of the folder in my documents folder .I want to create a copy of that file in some other folder with some other name.The code I am using is

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];

But this code is not working.Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-11-09 16:50:11

在:

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

In:

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

you’re not specifying the destination file name: ~/Documents/test/test1 is a directory.

You could do this instead:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文