2013-08-28 85 views
4

我正在編寫一個將刪除文件的應用程序。如果卷支持垃圾箱文件夾,我想將文件移動到那裏,否則只需移除它們。如何確定卷是否支持垃圾箱?

這可能只是我的搜索mojo失敗我,但我找不到如何做到這一點。

這裏的意圖是做類似的信息(僞):

if (itemURL on volume that supports Trash) 
    use trashItemAtURL:resultingItemURL:error: 
else 
    use removeItemAtURL:error: 

我知道recycleURLs:completionHandler:但是,對於SMB卷爲例,它提出了一個對話框有關文件如何(s)將被立即刪除。我想單獨處理這些文件,以便我可以更新顯示。我想我可以使用recycleURLs:completionHandler:如果文件系統事件適用於SMB卷,但我不確定他們這樣做。

Anyhoo ..是否有一些API我失蹤或一些推薦的技術,我只是不能似乎找到?

回答

3

一個不錯的人從蘋果開發論壇好容易找到了答案:

From the 10.8 Foundation release notes:

NSFileManager Trash APIs In Mac OS 10.8, NSFileManager has new methods to manage the Trash. The -[NSFileManager trashItemAtURL:resultingItemURL:error:] will attempt to move the item at the given URL to the trash, returning the resulting URL by reference. As part of this operation, the system may rename the file to avoid name conflicts; if so, the resultingItemURL will reflect this new name.

[snip]

Some volumes may not support a Trash folder, so these methods will report failure by returning NO or nil and an NSError with NSFeatureUnsupportedError. NSFeatureUnsupportedError is a new error code in the NSCocoaErrorDomain that indicates a failure to perform the requested operation because the feature is not supported, either because the file system lacks the feature, or required libraries are missing, or other similar reasons.

不知道爲什麼那個文件夾從來沒有想出了搜索。

+1

如果您需要在10.8之前運行的代碼,則可以使用不推薦使用的函數'FSPathMoveObjectToTrashSync'。 – JWWalker

+0

10.8對我來說很好,但對其他人來說這是一個很好的點。 – vagrant