看到一個字節,不要使用system()
。相反,使用-[NSFileManager moveItemAtPath:toPath:error:]
像這樣:
/* Find the user's Desktop directory. */
NSArray *results = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES/*expandTilde*/);
if (![results count]) return;
NSString *parentDir = [results objectAtIndex:0U];
/* Build the source path in a platform-independent manner. */
/* -pathWithComponents: will use the separator ('/', '\', or ':')
* appropriate for the platform. */
NSArray *srcParts = [NSArray arrayWithObjects:@"/", @"Volumes", @"Public", @"Directory*", (void *)nil];
NSString *src = [NSString pathWithComponents:srcParts];
/* Build the destination path. */
NSString *fnam = [src lastPathComponent];
NSString *dest = [parentDir stringByAppendingPathComponent:fnam];
/* Move |src| to |dest|. */
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = NULL;
BOOL ok = [fm moveItemAtPath:src toPath:dest error:&error];
if (!ok) {
[[NSApplication sharedApplication] presentError:error];
}
如果你不關心是平臺無關的,你可以硬編碼src
和dest
參數,直接執行移動,並通過75%縮短這個。
請注意,這將而不是做globbing - 它期望有一個名爲「Directory *」的目錄,其中星號是目錄名的最後部分。您可以使用<glob.h>
中定義的3210自己處理globbing。