【问题标题】:how to copy a file on the network in cocoa如何在可可中复制网络上的文件
【发布时间】:2012-11-30 04:29:45
【问题描述】:

我想将所选文件从我的计算机复制到同一网络上的另一台计算机。我尝试使用 NSFileManager 但没有成功。请问怎么做?

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:@"Documents/test"]];
NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:@"Shared/Test"]];
[fileManager copyItemAtPath:filePath toPath:filePath2 error:NULL];
[fileManager release];

【问题讨论】:

  • 如果网络共享是基于文件的,那么NSFileManager是要走的路,所以请发布更多关于“不成功”的细节。
  • 就像@trojanfoe 说的:你使用了什么代码,遇到了什么错误?
  • 该代码无法编译,因为您缺少终止 " 字符。
  • 如果你使用[NSFileManager defaultManager]而不是[[NSManager alloc] init]
  • trojanfoe: [[NSFileManager alloc] init] 是我认为 Apple 推荐的方式

标签: objective-c cocoa file-upload copy nsfilemanager


【解决方案1】:

2 条建议:

1) 根据文档,在这一行中,错误应该是“nil”而不是“NULL”

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

2) 也许代码没有找到文件。我注意到您在路径上没有任何文件扩展名(也许“test”应该是“test.txt”?)。即使您在 Finder 中看不到扩展名,大多数文件也有扩展名。获取有关文件的信息以检查其扩展名并在这种情况下修复代码。

【讨论】:

  • 感谢您的回复,但我正在使用 openPanel 来选择文件。我想做的是当按下打开按钮时,进度将开始。
  • 您是否尝试过 nil,如果在尝试 nil 后继续出现错误,您是否记录了错误?
  • 是的,但我在 [fileManager release] 行出现错误;它说 release in available.
  • 好的,然后从您的代码中删除发布行。还将“[[NSFileManager alloc] init]”更改为“[[[NSFileManager alloc] init] autorelease]”或“[NSFileManager defaultManager]”。无论如何,我总是使用 defaultManager 而不是 alloc/init,它可以顺利运行。
  • 我在删除发布线后工作!非常感谢您的帮助!
猜你喜欢
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多