- Home /
How to send a folder to the recycle bin via script
Directory.Delete() wipes folders from the disk. Is there any "normal" way to go about sending them to the recycle bin in Windows? All the libraries suggested on StackExchange aren't included with Unity, though that could be a viable answer if it's just a matter of bringing a dll into the project and using it?
Answer by DanielAllison · Jan 09, 2018 at 01:51 PM
If you want to go down the external library route then follow this guide from the Unity documentation that should cover everything you need to import a dll. Since you're not creating the dlls yourself, you can start from the "Using the DLL" section.
I was pointed to VisualBasic.FileIO, Shell32 and others, but yes they all required importation of DLLs, so this article helps. Thank you. https://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin https://social.msdn.microsoft.com/Forums/vstudio/en-US/05e3628d-5d08-4cea-8821-d5302139cc0d/how-to-access-the-recyclebin-using-c?forum=csharpgeneral
Based on the links you sent, it looks like using the $$anonymous$$icrosoft.VisualBasic assembly and replacing your Directory.Delete() line with FileSystem.DeleteDirectoy("SomeDirectory", UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin) would do the trick.
Here's the documentation for the method if it's helpful - https://msdn.microsoft.com/en-us/library/ms127971(v=vs.110).aspx
I've imported $$anonymous$$icrosoft.VisualBasic.dll which required me to import System.Deployment.dll as well. Unfortunately the files do not go to the recycle bin =[ It does delete them though.... I tried using the DeletePermanently and SendToRecycleBin options on both folders and files after testing they exist using the FileSystem, but they just disappear =[ ??
I don't think there is any other/easier option is there? I mean I'd pay 5$ for an asset to do it for me honestly, a quick search really doesn't produce results though.
Answer by Honorsoft · Jan 10, 2018 at 04:09 PM
There is a built-in UnityEngine function, not requiring DLL's. docs.unity3d.com/ScriptReference/Windows.Directory.Delete
For example:
using UnityEngine.Windows.Directory
then...
UnityEngine.Windows.Directory.Delete("FOLDER PATH");
Not sure if the 'using' is right, I just saw this in this documentation when looking for an alternative to the System.IO.Directory method.
I don't have a Directory option via using nor typing out the whole reference? There is only Speech under UnityEngine.Windows? I'll look into it though. FYI, my target platform is set to Windows... Looks like I need to target Universal Windows Platform, not just PC/Linux/$$anonymous$$ac... fair for now... installing the addon...