public static bool DeleteToRecycleBin(string filePath, FileOperationFlags flags) { if (Platform.IsWindows) { if (!File.Exists(filePath) && !Directory.Exists(filePath)) { return(false); } // alternative using visual basic dll: // FileSystem.DeleteDirectory(item.FolderPath,UIOption.OnlyErrorDialogs), RecycleOption.SendToRecycleBin); //moves it to the recyle bin try { var shf = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = filePath + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperation(ref shf); return(!shf.fAnyOperationsAborted); } catch (Exception) { return(false); } } // On Linux we'll have to move the file to $XDG_DATA_HOME/Trash/files and create // a filename.trashinfo file in $XDG_DATA_HOME/Trash/info that contains the original // filepath and the deletion date. See http://stackoverflow.com/a/20255190 // and http://freedesktop.org/wiki/Specifications/trash-spec/. // Environment.SpecialFolder.LocalApplicationData corresponds to $XDG_DATA_HOME. // move file or directory if (Directory.Exists(filePath) || File.Exists(filePath)) { var trashPath = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData), "Trash"); var trashedFileName = Path.GetRandomFileName(); if (!Directory.Exists(trashPath)) { // in case the trash bin doesn't exist we create it. This can happen e.g. // on the build machine Directory.CreateDirectory(Path.Combine(trashPath, "files")); Directory.CreateDirectory(Path.Combine(trashPath, "info")); } var recyclePath = Path.Combine(Path.Combine(trashPath, "files"), trashedFileName); WriteTrashInfoFile(trashPath, filePath, trashedFileName); // Directory.Move works for directories and files DirectoryHelper.Move(filePath, recyclePath); return(true); } return(false); }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags) { //SHFileOperation needs the full directory to be able to move files //to the recyclebin on windows 2k/XP string currentDir = Directory.GetCurrentDirectory(); if (!path.StartsWith(currentDir)) { path = Path.Combine(currentDir, path); } try { SHFILEOPSTRUCT fs = new SHFILEOPSTRUCT(); fs.wFunc = FileOperationType.FO_DELETE; // important to double-terminate the string. fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; SHFileOperation(ref fs); return(true); } catch (Exception) { return(false); } }
//-------------------------------------------------------------------------------------------------------------- /// <summary> /// Send the specified file or directory to the recycle bin. /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> //-------------------------------------------------------------------------------------------------------------- public static bool Send(string path, FileOperationFlags flags = FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING) { try { if (IsWOW64Process()) { var fs = new ShFileOpStructX64 { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', // important to double-terminate the string. fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperationx64(ref fs); } else { var fs = new ShFileOpStructX86 { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', // important to double-terminate the string. fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperationx86(ref fs); } return(true); } catch { return(false); } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> private static bool SendToRecycleBin(string path, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; var returnCode = (SHFileOperationReturnCode)NativeMethods.SHFileOperation(ref fs); switch (returnCode) { case SHFileOperationReturnCode.SUCCESSFUL: return(true); case SHFileOperationReturnCode.ERROR_SHARING_VIOLATION: throw new FileInUsingException(path); case SHFileOperationReturnCode.DE_ERROR_MAX: throw new FileInUsingException(path); case SHFileOperationReturnCode.ERRORONDEST: throw new IOException("An unspecified error occurred on the destination."); default: throw new NotImplementedException("Not supported SHFileOperation return code: " + returnCode); } }
private static int SHFileOperation(FileOperationType func, string path, FileOperationFlags flags, IntPtr parent) { if (Marshal.SizeOf(typeof(IntPtr)) == 4) { var fs = new SHFILEOPSTRUCT32 { hwnd = parent, wFunc = func, pFrom = path, fFlags = flags }; return(SHFileOperation32(ref fs)); } else { var fs = new SHFILEOPSTRUCT64 { hwnd = parent, wFunc = func, pFrom = path, fFlags = flags }; return(SHFileOperation64(ref fs)); } }
/// <summary> /// Sends a file to the Recycle Bin /// </summary> /// <param name="path">The path of the directory or file to send to the Recycle Bin</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> private static bool SendToRecycleBin(string path, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; var returnCode = (SHFileOperationReturnCode)NativeMethods.SHFileOperation(ref fs); switch (returnCode) { case SHFileOperationReturnCode.SUCCESSFUL: return(true); case SHFileOperationReturnCode.ERROR_SHARING_VIOLATION: throw new IOException($"The process cannot access the file '{Path.GetFullPath(path)}' because it is being used by another process."); case SHFileOperationReturnCode.DE_ERROR_MAX: throw new IOException($"The length of target path '{Path.GetFullPath(path)}' is over MAX_PATH"); case SHFileOperationReturnCode.ERRORONDEST: throw new IOException("An unspecified error occurred on the destination."); default: throw new NotImplementedException("Not supported SHFileOperation return code: " + returnCode); } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Delete(string path, FileOperationFlags flags) { try { if (IsWOW64Process()) { SHFILEOPSTRUCT_x64 fs = new SHFILEOPSTRUCT_x64(); fs.wFunc = FileOperationType.FO_DELETE; // important to double-terminate the string. fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; SHFileOperation_x64(ref fs); } else { SHFILEOPSTRUCT_x86 fs = new SHFILEOPSTRUCT_x86(); fs.wFunc = FileOperationType.FO_DELETE; // important to double-terminate the string. fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; SHFileOperation_x86(ref fs); } return(true); } catch { return(false); } }
public static bool CallSH(FileOperationType type, string[] from, string to, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT(); fs.wFunc = type; if (from.Length > 0) fs.pFrom = string.Join("\0", from) + "\0\0"; if (to.Length > 0) fs.pTo = to + "\0\0"; fs.fFlags = flags; return SHFileOperation(ref fs) == 0; }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Delete(string path, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; return(0 == SHFileOperation(ref fs)); }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags = FileOperationFlags.FOF_ALLOWUNDO | FileOperationFlags.FOF_WANTNUKEWARNING) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = flags }; return(SHFileOperation(ref fs) == 0); }
private static bool DeleteFile(string path, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = flags }; SHFileOperation(ref fs); return(true); }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool SendTo(string path, string path2, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_MOVE, pFrom = path + '\0' + '\0', pTo = path2 + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperation(ref fs); return(true); }
private static bool DeleteFile(string[] path, FileOperationFlags flags) { if (path == null || path.All(x => x == null)) return false; try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = string.Join("\0", path.Where(x => x != null)) + "\0\0", fFlags = flags }; SHFileOperation(ref fs); return true; } catch (Exception) { return false; } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags) { try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperation(ref fs); return(true); } catch (Exception) { return(false); } }
/// <summary>Deletes or recycles the file based on the conditional value.</summary> /// /// <param name="file">The path of the file to delete.</param> /// <param name="recycle">True if the file should be recycled.</param> /// <returns>True if the operation was successful.</returns> public bool DeleteOrRecycleFile(string file, bool recycle) { return(ui.Invoke(() => { FileOperationFlags flags = FileOperationFlags.None; if (recycle) { flags = FileOperationFlags.AllowUndo | FileOperationFlags.WarnFilesTooBigForRecycleBin; } SHFileOperationStruct fileOp = new SHFileOperationStruct { wFunc = FileOperationFunc.Delete, pFrom = file + '\0' + '\0', fFlags = flags, }; return !SHFileOperation(ref fileOp); })); }
public static bool CallSH(FileOperationType type, string[] from, string to, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT(); fs.wFunc = type; if (from.Length > 0) { fs.pFrom = string.Join("\0", from) + "\0\0"; } if (to.Length > 0) { fs.pTo = to + "\0\0"; } fs.fFlags = flags; return(SHFileOperation(ref fs) == 0); }
/// <summary> /// Эта функция отправляет файл или папку в корзину /// </summary> /// <param name="path">Полное имя файла или папки, которую нужно удалить</param> /// <param name="flags"> FileOperationFlags в дополнение к флагу FOF_ALLOWUNDO,FOF_SILENT,FOF_NOCONFIRMATION который уже задан</param> public static void DeleteToRecycleBin(string path, FileOperationFlags flags) { int result; bool operationsAborted; //если 64-битная система if (IsWOW64Process()) { //создаем новую структуру SHFILEOPSTRUCT_x64 fs = new SHFILEOPSTRUCT_x64(); //указываем, что файл будет удаляться fs.wFunc = FileOperationType.FO_DELETE; //Непонятно, почему, но если функции передать структуру, в которой указан "нормальный" //путь, то произойдет ошибка fs.pFrom = path + '\0' + '\0'; //установка флагов fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | FileOperationFlags.FOF_SILENT | FileOperationFlags.FOF_NOCONFIRMATION | flags; //выполнение функции result = SHFileOperation_x64(ref fs); operationsAborted = fs.fAnyOperationsAborted; } else { //система 32-битная. Здесь все так же, как для 64-битной системы, только структура и //функция уже 32-битные SHFILEOPSTRUCT_x86 fs = new SHFILEOPSTRUCT_x86(); fs.wFunc = FileOperationType.FO_DELETE; fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | FileOperationFlags.FOF_SILENT | FileOperationFlags.FOF_NOCONFIRMATION | flags;//без подтверждения пользователя(ushort)(0x0004 | 0x0040 | 0x0010); result = SHFileOperation_x86(ref fs); operationsAborted = fs.fAnyOperationsAborted; } if (result != 0) { // произошла ошибка Console.WriteLine("Delete to RecycleBin Error"); throw new Exception("Delete to RecycleBin Error (result = " + result + ")"); } else { if (operationsAborted) { // операция была прервана Console.WriteLine("Remove operation aborted"); } } }
private static bool DeleteFile(string path, FileOperationFlags flags) { try { SHFILEOPSTRUCT sHFILEOPSTRUCT = default(SHFILEOPSTRUCT); sHFILEOPSTRUCT.wFunc = FileOperationType.FO_DELETE; sHFILEOPSTRUCT.pFrom = path + "\0\0"; sHFILEOPSTRUCT.fFlags = flags; SHFILEOPSTRUCT FileOp = sHFILEOPSTRUCT; SHFileOperation(ref FileOp); return(true); } catch (Exception) { return(false); } }
public static bool Delete(IEnumerable <string> file_list, FileOperationFlags flags, IntPtr parent = default(IntPtr)) { var files = new StringBuilder(); foreach (var file in file_list) { files.Append(file); files.Append('\0'); } if (0 == files.Length) { return(false); } files.Append('\0'); return(0 == SHFileOperation(FileOperationType.FO_DELETE, files.ToString(), FileOperationFlags.FOF_ALLOWUNDO | flags, parent)); }
private static bool ShellOperation(FileOperationType operationType, FileOperationFlags operationFlags, string[] sources, string destination) { int num; SHFILEOPSTRUCT lpFileOp = GetShellOperationInfo(operationType, operationFlags, sources, destination); num = SafeNativeMethods.SHFileOperation(ref lpFileOp); SafeNativeMethods.SHChangeNotify(0x2381f, 3, IntPtr.Zero, IntPtr.Zero); if (lpFileOp.fAnyOperationsAborted) { return(false); } else if (num != 0) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } return(true); }
private static bool Send(string path, FileOperationFlags flags) { try { SHFILEOPSTRUCT sHFILEOPSTRUCT = default(SHFILEOPSTRUCT); sHFILEOPSTRUCT.wFunc = FileOperationType.FO_DELETE; sHFILEOPSTRUCT.pFrom = path + '\0' + '\0'; sHFILEOPSTRUCT.fFlags = (FileOperationFlags.FOF_ALLOWUNDO | flags); SHFILEOPSTRUCT FileOp = sHFILEOPSTRUCT; SHFileOperation(ref FileOp); return(true); } catch (Exception) { return(false); } }
private static bool DeleteFile(string path, FileOperationFlags flags) { try { SHFILEOPSTRUCT fileOp = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = flags }; SHFileOperation(ref fileOp); return(true); } catch (Exception) { return(false); } }
private static bool DeleteFile(string path, FileOperationFlags flags) { try { SHFILEOPSTRUCT fileOp = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = flags }; SHFileOperation(ref fileOp); return true; } catch (Exception) { return false; } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to AllowUndo</param> private static bool Send(string path, FileOperationFlags flags) { try { var fs = new FileOperationStructure { wFunc = FileOperationType.Delete, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.AllowUndo | flags }; _ = SHFileOperation(ref fs); return(true); } catch (Exception) { return(false); } }
private static bool Send(string path, FileOperationFlags flags) { try { SHFILEOPSTRUCT fileOp = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = (FileOperationFlags)((ushort)(FileOperationFlags.FOF_ALLOWUNDO | flags)) }; SHFileOperation(ref fileOp); return(true); } catch (Exception) { return(false); } }
private static bool DeleteFile(string[] path, FileOperationFlags flags) { if (path == null || path.All(x => x == null)) { return(false); } try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = string.Join("\0", path.Where(x => x != null)) + "\0\0", fFlags = flags }; SHFileOperation(ref fs); return(true); } catch (Exception) { return(false); } }
public static bool Perform(string path, FileOperationFlags flags) { try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperation(ref fs); return true; } catch (Exception) { return false; } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool SendToRecycleBin(string path, FileOperationFlags flags) { try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = (FileOperationFlags)0x40 | flags // This combines the UNDO flag with our chosen flags. }; _ = SHFileOperation(ref fs); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Send file to recycle bin. /// Returns true on success. /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags) { // TODO find out what this does if recycle bin is disabled try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; int error = SHFileOperation(ref fs); return(error == 0); } catch (Exception) { return(false); } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags) { try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperation(ref fs); return(true); } catch (Exception e) { Debug.WriteLine("Deletion error: " + path + " " + e.Message); return(false); } }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags) { try { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; SHFileOperation(ref fs); return(true); } catch (Exception e) { MessageBox.Show(e.Message + Environment.NewLine + Environment.NewLine + e.StackTrace, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } return(false); }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name = "path">Location of directory or file to recycle</param> /// <param name = "flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Send(string path, FileOperationFlags flags) { try { var fs = new SHFileOpStruct { wFunc = FileOperationType.Delete, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.AllowUndo | flags }; // important to double-terminate the string. SHFileOperation(ref fs); return(true); } catch (Exception) { return(false); } }
// <summary> /// Эта функция отправляет файл или папку в корзину /// </summary> /// <param name="path">Полное имя файла или папки, которую нужно удалить</param> /// <param name="flags">FileOperationFlags в дополнение к флагу FOF_ALLOWUNDO, /// который уже задан</param> public static bool SendToRecycle(string path, FileOperationFlags flags) { try { //если 64-битная система if (IsWOW64Process()) { //создаем новую структуру SHFILEOPSTRUCT_x64 fs = new SHFILEOPSTRUCT_x64(); //указываем, что файл будет удаляться fs.wFunc = FileOperationType.FO_DELETE; //Непонятно, почему, но если функции передать структуру, в которой указан "нормальный" //путь, то произойдет ошибка (на этом форуме четыре года назад в разделе Visual C++ //спрашивали как раз по этому поводу) fs.pFrom = path + '\0' + '\0'; //установка флагов fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; //выполнение функции SHFileOperation_x64(ref fs); } else { //система 32-битная. Здесь все так же, как для 64-битной системы, только структура и //функция уже 32-битные SHFILEOPSTRUCT_x86 fs = new SHFILEOPSTRUCT_x86(); fs.wFunc = FileOperationType.FO_DELETE; fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; SHFileOperation_x86(ref fs); } //все прошло успешно, возвращаем true return(true); } catch { //произошла какая-то ошибка, возвращаем false return(false); } }
public static bool Delete(IEnumerable <string> file_list, FileOperationFlags flags) { var files = new StringBuilder(); foreach (var file in file_list) { files.Append(file); files.Append('\0'); } if (0 == files.Length) { return(false); } files.Append('\0'); var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = files.ToString(), fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; return(0 == SHFileOperation(ref fs)); }
/// <summary> /// Move file to recycle bin, don't display any dialogs or errors. /// </summary> /// <param name="filePath">Path of the file that should be moved to recycle bin</param> public static bool MoveFileToRecycleBin(string filePath) { try { FileOperationFlags operationFlags = FileOperationFlags.AllowUndo | FileOperationFlags.NoProgressDialogBox | FileOperationFlags.NoConfirmation | FileOperationFlags.NoError; var fileOperationData = new FileOperationStruct { ActionToPerform = 3, SourcePath = filePath, OperationFlags = operationFlags }; SHFileOperation(ref fileOperationData); return(true); } catch (Exception) { return(false); } }
public void SetOperationFlags(FileOperationFlags operationFlags) { _fileOperation.SetOperationFlags(operationFlags); }
private static bool Send(string path, FileOperationFlags flags) { try { SHFILEOPSTRUCT fileOp = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = (FileOperationFlags)((ushort)(FileOperationFlags.FOF_ALLOWUNDO | flags)) }; SHFileOperation(ref fileOp); return true; } catch (Exception) { return false; } }
public static bool Delete(string[] paths, FileOperationFlags flags) { return CallSH(FileOperationType.FO_DELETE, paths, "", flags); }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Delete(string path, FileOperationFlags flags) { try { if (IsWOW64Process()) { SHFILEOPSTRUCT_x64 fs = new SHFILEOPSTRUCT_x64(); fs.wFunc = FileOperationType.FO_DELETE; // important to double-terminate the string. fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; SHFileOperation_x64(ref fs); } else { SHFILEOPSTRUCT_x86 fs = new SHFILEOPSTRUCT_x86(); fs.wFunc = FileOperationType.FO_DELETE; // important to double-terminate the string. fs.pFrom = path + '\0' + '\0'; fs.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags; SHFileOperation_x86(ref fs); } return true; } catch { return false; } }
public static bool Delete(IEnumerable<string> file_list, FileOperationFlags flags) { var files = new StringBuilder(); foreach (var file in file_list) { files.Append (file); files.Append ('\0'); } if (0 == files.Length) return false; files.Append ('\0'); var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = files.ToString(), fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; return 0 == SHFileOperation (ref fs); }
/// <summary> /// Send file to recycle bin /// </summary> /// <param name="path">Location of directory or file to recycle</param> /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param> public static bool Delete(string path, FileOperationFlags flags) { var fs = new SHFILEOPSTRUCT { wFunc = FileOperationType.FO_DELETE, pFrom = path + '\0' + '\0', fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags }; return 0 == SHFileOperation (ref fs); }