FSFindFolder for find a file/folder in trash
-
06-07-2021 - |
Frage
Got a problem for how to determine file/folder in trash. There's some solution on Internet, one of those is FSFindFolder. which can be used something like this:
FSFindFolder(kUserDomain, kTrashFolderType, true, &trashRef);
but still don't know how to pass the path of file that I want to determine using this method
Lösung
The last parameter of FSFindFolder
is a FSRef
, which can be converted to a CFURLRef
and to a NSURL
:
FSRef trashRef;
FSFindFolder(kUserDomain, kTrashFolderType, true, &trashRef);
CFURLRef urlRef = CFURLCreateFromFSRef(NULL, &trashRef);
NSURL *trashUrl = CFBridgingRelease(urlRef);
NSString *trashPath = [trashUrl path];
NSLog(@"trash folder: %@", trashPath);
(I have omitted the error checking for sake of brevity. I have also assumed that you use automatic reference counting.)
Now you can check if a given file is in the trash folder:
NSString *theFile = ...; // your file here
NSString *fileInTrash = [trashPath stringByAppendingPathComponent:theFile];
BOOL isInTrash = [[NSFileManager defaultManager] fileExistsAtPath:fileInTrash];
NSLog(@"is in trash: %d", isInTrash);
NOTE: On Mac OS X 10.8, FSFindFolder
is deprecated, and you can use the following code to find the trash folder:
NSString *trashPath = [NSSearchPathForDirectoriesInDomains(NSTrashDirectory, NSUserDomainMask, YES) lastObject];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow