开发者

Move directory to trash

开发者 https://www.devze.com 2023-01-15 11:27 出处:网络
I need to move a directory, including its content, to the trash. I found NSWorkspaceRecycleOperation in the documentation, and wrote this code:

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?


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

精彩评论

暂无评论...
验证码 换一张
取 消