public static bool DeleteToRecycleBin(string path) { if (string.IsNullOrEmpty(path)) { return(false); } // remove end \\ char otherwise SHFileOperation() will failed if (path.EndsWith(@"\")) { path = path.Substring(0, path.Length - 1); } if (!File.Exists(path) && !Directory.Exists(path)) { return(false); } try { _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT(); pm.wFunc = FO_DELETE; pm.pFrom = path + '\0'; pm.pTo = null; pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI; return(SHFileOperation(pm) == 0); } catch (Exception ee) { Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message); Trace.WriteLine("### " + ee.StackTrace); return(false); } }
/// <summary> /// 删除文件 /// </summary> /// <param name="path"></param> /// <returns></returns> public static int Delete(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)); }
private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);
private static extern int SHFileOperation([System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] _SHFILEOPSTRUCT str);
public static int Delete(List<string> path) { _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT(); pm.wFunc = FO_DELETE; pm.pFrom = path[0]; for (int i = 1; i < path.Count; i++) { pm.pFrom += '\0' + path[i]; } pm.pFrom += '\0'; pm.pTo = null; pm.fFlags = FOF_ALLOWUNDO | FOF_WANTNUKEWARNING; return SHFileOperation(pm); }
public static bool DeleteToRecycleBin(string path) { if (string.IsNullOrEmpty(path)) return false; // remove end \\ char otherwise SHFileOperation() will failed if (path.EndsWith(@"\")) path = path.Substring(0, path.Length - 1); if (!File.Exists(path) && !Directory.Exists(path)) return false; try { _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT(); pm.wFunc = FO_DELETE; pm.pFrom = path + '\0'; pm.pTo = null; pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI; return (SHFileOperation(pm) == 0); } catch (Exception ee) { Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message); Trace.WriteLine("### " + ee.StackTrace); return false; } }