如何将文件复制到可移植驱动器根? [英] How to copy a file to portable drive root with cocoa?

查看:130
本文介绍了如何将文件复制到可移植驱动器根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过下面的

[[NSFileManager defaultManager] copyItemAtPath:@"whatever.txt"
 toPath:@"/Volumes/MyDrive" error:&copyError];

我的错误操作无法完成。文件存在

This gives me the error "The operation couldn’t be completed. File exists"

如果我尝试将其复制到/ Volumes / MyDrive / testFolder,一切复制到testFolder精细。

If I try to copy it to "/Volumes/MyDrive/testFolder" everything copies to testFolder just fine.

推荐答案


我试过下列

I've tried the following

[[NSFileManager defaultManager] copyItemAtPath:@"whatever.txt"
 toPath:@"/Volumes/MyDrive" error:&copyError];

这会给我错误操作无法完成。文件存在

This gives me the error "The operation couldn’t be completed. File exists"

首先,KennyTM已经告诉你,错误信息告诉你一个可能的原因:文件已经存在。目标不能存在;您必须删除它或为目标指定不同的名称。

First, as KennyTM already told you, the error message is telling you one possible cause of the problem: The file already exists. The destination must not exist; you must either delete it or give a different name for the destination.

其次,还有另一个可能的原因:您只指定目标文件夹,而不是完整目标路径。您必须指定完整的目标路径,包括目标文件名。 Quoth文档:

Second, there is another possible cause of the problem: You only specified the destination folder, not the complete destination path. You must specify the complete destination path, including the destination filename. Quoth the documentation:

当复制文件时,目标路径必须以文件名结尾 - 没有明确采用源文件名。

"When a file is being copied, the destination path must end in a filename—there is no implicit adoption of the source filename."

如果您希望副本具有路径名 /Volumes/MyDrive/whatever.txt ,那就是您需要传递的路径名。

If you want the copy to have the pathname /Volumes/MyDrive/whatever.txt, that's the pathname you need to pass.

此外,在尝试查看错误对象之前,请不要忘记检查副本是否成功。

Also, don't forget to check whether the copy succeeded before you attempt to look at the error object. You should only look at the error object if the copy failed.


如果我尝试将其复制到/ Volumes / MyDrive / testFolder一切复制到testFolder就好了。

If I try to copy it to "/Volumes/MyDrive/testFolder" everything copies to testFolder just fine.

我想你会发现 testFolder 实际上是一个文件特定的,它是whatever.txt的副本。

I think you'll find that testFolder is, in fact, a file—specifically, it's the copy of whatever.txt.

这篇关于如何将文件复制到可移植驱动器根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆