admin 管理员组

文章数量: 1087677

C# 之 删除文件到回收站

C# 使用Delect方法无法找回,使用下面的方法可以将文件移到回收站:

        Private const int FO_DELETE = 0x3;private const ushort FOF_NOCONFIRMATION = 0x10;private const ushort FOF_ALLOWUNDO = 0x40;[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public class _SHFILEOPSTRUCT{public IntPtr hwnd;public UInt32 wFunc;public string pFrom;public string pTo;public UInt16 fFlags;public Int32 fAnyOperationsAborted;public IntPtr hNameMappings;public string lpszProgressTitle;}/// <summary>/// 移动到回收站中/// </summary>/// <param name="path"></param>/// <returns></returns>public static int MoveToTrash(string path){_SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();pm.wFunc = FO_DELETE;pm.pFrom = path + '\0';pm.pTo = null;pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;return SHFileOperation(pm);}

本文标签: C 之 删除文件到回收站