Move directory to trash

流过昼夜 提交于 2020-01-14 04:20:25

问题


I need to move a directory, including its content, to the trash. I found NSWorkspaceRecycleOperation in the documentation, and wrote this code:

NSString *path = [NSString stringWithString:@"/Users/test/Desktop/test"];

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation 
                                             source:path 
                                        destination:@"" 
                                              files:dirContents 
                                                tag:nil];

It moves all the content to the trash, but not the directory itself. So, how can I do this?


回答1:


You are currently only performing the recycle operation on the directories contents. Given a directory dir to trash, use something like the following instead:

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation 
                               source:[dir stringByDeletingLastPathComponent]
                               destination:@"" 
                               files:[NSArray arrayWithObject:[dir lastPathComponent]]
                               tag:nil];


来源:https://stackoverflow.com/questions/3665660/move-directory-to-trash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!