- usingSystem.Runtime.InteropServices;
- privateconstintFO_DELETE=0x3;
- privateconstushortFOF_NOCONFIRMATION=0x10;
- privateconstushortFOF_ALLOWUNDO=0x40;
- [DllImport("shell32.dll")]
- privatestaticexternintSHFileOperation([In,Out]_SHFILEOPSTRUCTstr);
- [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
- publicclass_SHFILEOPSTRUCT
- {
- publicIntPtrhwnd;
- publicUInt32wFunc;
- publicstringpFrom;
- publicstringpTo;
- publicUInt16fFlags;
- publicInt32fAnyOperationsAborted;
- publicIntPtrhNameMappings;
- publicstringlpszProgressTitle;
- }
- publicstaticintDeleteToRecycleBin(stringpath)
- {
- _SHFILEOPSTRUCTpm=new_SHFILEOPSTRUCT();
- pm.wFunc=FO_DELETE;
- pm.pFrom=path+'/0';
- pm.pTo=null;
- pm.fFlags=FOF_ALLOWUNDO|FOF_NOCONFIRMATION;
- returnSHFileOperation(pm);
- }
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- DeleteToRecycleBin(@"c:/temp/temp.txt");
- }
C#实现删除文件放入回收站
最新推荐文章于 2025-09-10 09:09:00 发布
本文介绍了一种使用C#实现将指定路径的文件直接发送到回收站的方法。通过调用shell32.dll中的SHFileOperation函数,并定义了相关的结构体_SHFILEOPSTRUCT来完成操作。此代码示例展示了如何设置参数以静默方式删除文件,同时允许用户从回收站还原。
335

被折叠的 条评论
为什么被折叠?



