将文件发送到回收站
目前我正在使用以下函数
file.Delete();
但是如何使用此函数将文件发送到回收站而不是直接删除它?
Currently I'm using the following function
file.Delete();
But how can I use this function to send a file to the recycle bin instead of just deleting it outright?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 FileSystem.DeleteFile 并指定右RecycleOption。
虽然这适用于 UI 交互式应用程序,但它不适用于非 UI 交互式应用程序(例如 Windows 服务应用程序)。
Use FileSystem.DeleteFile and specify the right RecycleOption.
While this will work with UI Interactive Apps, it will not work with non UI interactive apps like a Windows Service app.
注意:这也不适用于非 UI 交互式应用程序(例如 Windows 服务)
此包装器可以为您提供所需的功能:
NOTE: This also does not work with non UI Interactive apps like Windows Services
This wrapper can provide you needed functionality:
来自 MSDN :
添加对 Microsoft.VisualBasic 程序集的引用。需要的类可以在这个库中找到。
将此 using 语句添加到文件顶部
using Microsoft.VisualBasic.FileIO
;使用
FileSystem.DeleteFile
删除文件,可以选择是否指定回收站。使用 FileSystem.DeleteDirectory 删除目录,并可以选择是否将其发送到回收站。
From MSDN:
Add a reference to Microsoft.VisualBasic assembly. The needed class is found in this library.
Add this using statement to the top of the file
using Microsoft.VisualBasic.FileIO
;Use
FileSystem.DeleteFile
to delete a file, it has the option to specify recycle bin or not.Use
FileSystem.DeleteDirectory
to delete a directory with the option to specify to send it to the recycle bin or not.以下解决方案比其他解决方案更简单:
您可以使用此库使用回收站的其他功能。
首先,不要忘记添加库“Microsoft Shell Controls And Automation”(从 COM 菜单),以便能够使用
Shell32
命名空间。它将动态链接到您的项目,而不是与您的程序一起编译。The following solution is simpler than the other ones:
You can use other functionalities of the recycle bin using this library.
First, don't forget to add the library "Microsoft Shell Controls And Automation" (from the COM menu), to be able to use the
Shell32
namespace. It will be dynamically linked to your project, instead of being compiled along with your program.不幸的是,您需要借助 Win32 API 将文件删除到回收站。尝试以下基于 这篇文章。它通过 Windows Shell 使用通用的
SHFileOperation
函数进行文件系统操作。定义以下内容(在实用程序类中可能是最好的)。
要使用它来删除文件,将其发送到回收站,您需要类似以下内容:
Unfortunately you need to resort to the Win32 API to remove a file to the Recycle Bin. Try the following code, based on this post. It makes use of the generic
SHFileOperation
function for file system operations via the Windows Shell.Define the following (in a utilities class is probably best).
And to use it to delete a file, sending it to the Recycle Bin, you want something like:
为此有内置库。
首先添加引用Microsoft.VisualBasic
然后添加此代码:
我找到了这个 此处。
There is built-in library for this .
First add reference Microsoft.VisualBasic
Then add this code :
I have found this here .
您可以 DllImport
SHFileOperation
来执行此操作。You can DllImport
SHFileOperation
to do this.我使用这个扩展方法,然后我可以只使用 DirectoryInfo 或 FileInfo 并将其删除。
如何调用它的示例可能是这样的:
I use this extension method, then I can just use a DirectoryInfo or FileInfo and delete that.
a sample how to call it could be this:
这是一个简单的解决方案,应该适用于从 .NET Framework 4 到最新的 .NET Core(当前为 8),无需任何参考。从技术上讲,它与 user4584267 的相同,但不需要创建 COM 互操作文件,因为它使用 C#
dynamic
关键字:Here is a simple solution that should work from .NET Framework 4 to latest .NET Core (currently 8) without any reference needed. It's technically the same as user4584267's one but w/o the need to create a COM interop file, since it uses C#
dynamic
keyword: