ref SHFILEOPSTRUCT64 lpFileOp); // Address of an SHFILEOPSTRUCT structure that contains information // this function needs to carry out the specified operation. This // parameter must contain a valid value that is not NULL. You are // responsibile for validating the value. If you do not validate it, // you will experience unexpected results. public static Int32 SHFileOperation(ref SHFILEOPSTRUCT lpFileOp) { MachineType mt = GetMachineType(); Int32 result; switch (mt) { case MachineType.win32: SHFILEOPSTRUCT32 fos32 = new SHFILEOPSTRUCT32(lpFileOp); result = SHFileOperation32(ref fos32); lpFileOp.CopyFrom(fos32); break; case MachineType.win64: SHFILEOPSTRUCT64 fos64 = new SHFILEOPSTRUCT64(lpFileOp); result = SHFileOperation64(ref fos64); lpFileOp.CopyFrom(fos64); break; default: throw new ArgumentException("Hell, what kind of computer are you using? It's not 32 and not 64 bit"); } return(result); }
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)); } }
public void moveToRecycle(string path) { SHFILEOPSTRUCT64 fileop = new SHFILEOPSTRUCT64(); fileop.wFunc = FO_DELETE; fileop.pFrom = path + '\0' + '\0'; fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; SHFileOperation(ref fileop); }
public void CopyFrom(SHFILEOPSTRUCT64 initializer) { this.hwnd = initializer.hwnd; this.wFunc = initializer.wFunc; this.pFrom = initializer.pFrom; this.pTo = initializer.pTo; this.fFlags = initializer.fFlags; this.fAnyOperationsAborted = initializer.fAnyOperationsAborted; this.hNameMappings = initializer.hNameMappings; this.lpszProgressTitle = initializer.lpszProgressTitle; }
public static void DeleteToRecycleBin(params string[] paths) { int result; var hWnd = Process.GetCurrentProcess().MainWindowHandle; var wFunc = FILEOP_Func.FO_DELETE; var fFlags = FILEOP_Flags.FOF_SILENT | FILEOP_Flags.FOF_NOCONFIRMATION | FILEOP_Flags.FOF_ALLOWUNDO; var pFrom = string.Concat(paths.Select(p => $"{p}\0")) + "\0"; var lpszProgressTitle = string.Empty + "\0"; if (IntPtr.Size == 8) { var operation = new SHFILEOPSTRUCT64 { hWnd = hWnd, wFunc = wFunc, fFlags = fFlags, pFrom = pFrom, pTo = null, lpszProgressTitle = lpszProgressTitle, }; result = NativeMethods.SHFileOperation64(ref operation); } else { var operation = new SHFILEOPSTRUCT32 { hWnd = hWnd, wFunc = wFunc, fFlags = fFlags, pFrom = pFrom, pTo = null, lpszProgressTitle = lpszProgressTitle, }; result = NativeMethods.SHFileOperation32(ref operation); } switch (result) { case 0: break; } }
// Effective Function public static int SHFileOperation(SHFILEOPSTRUCT lpFileOp) { if (!Is64Bit()) { return(SHFileOperation32(lpFileOp)); } var shfileopstruct1 = new SHFILEOPSTRUCT64(); shfileopstruct1.hwnd = lpFileOp.hwnd; shfileopstruct1.wFunc = lpFileOp.wFunc; shfileopstruct1.pFrom = lpFileOp.pFrom; shfileopstruct1.pTo = lpFileOp.pTo; shfileopstruct1.fFlags = lpFileOp.fFlags; shfileopstruct1.fAnyOperationsAborted = lpFileOp.fAnyOperationsAborted; shfileopstruct1.hNameMappings = lpFileOp.hNameMappings; shfileopstruct1.lpszProgressTitle = lpFileOp.lpszProgressTitle; var ret = SHFileOperation64(shfileopstruct1); lpFileOp.fAnyOperationsAborted = shfileopstruct1.fAnyOperationsAborted; return(ret); }
static int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp) { if (IntPtr.Size == 4) { return(SHFileOperation32(ref lpFileOp)); } SHFILEOPSTRUCT64 shfileopstruct = new SHFILEOPSTRUCT64(); shfileopstruct.hwnd = lpFileOp.hwnd; shfileopstruct.wFunc = lpFileOp.wFunc; shfileopstruct.pFrom = lpFileOp.pFrom; shfileopstruct.pTo = lpFileOp.pTo; shfileopstruct.fFlags = lpFileOp.fFlags; shfileopstruct.fAnyOperationsAborted = lpFileOp.fAnyOperationsAborted; shfileopstruct.hNameMappings = lpFileOp.hNameMappings; shfileopstruct.lpszProgressTitle = lpFileOp.lpszProgressTitle; int num2 = SHFileOperation64(ref shfileopstruct); lpFileOp.fAnyOperationsAborted = shfileopstruct.fAnyOperationsAborted; return(num2); }
//private const int FOF_NOCONFIRMATION = 0x10; //No prompt dialogs public static bool DeleteFileToRecycleBin(string filePath) { int retVal; if (IntPtr.Size == 4) //32 bit { var shf = new SHFILEOPSTRUCT32(); shf.wFunc = FO_DELETE; shf.fFlags = FOF_ALLOWUNDO; shf.pFrom = filePath + "\0"; retVal = SHFileOperation32(ref shf); } else //64 bit (almost exactly the same, but without "Pack = 1" set. { var shf = new SHFILEOPSTRUCT64(); shf.wFunc = FO_DELETE; shf.fFlags = FOF_ALLOWUNDO; shf.pFrom = filePath + "\0"; retVal = SHFileOperation64(ref shf); } return(retVal == 0); }
public static extern Int32 SHFileOperation64( ref SHFILEOPSTRUCT64 lpFileOp);
// this function needs to carry out the specified operation. This // parameter must contain a valid value that is not NULL. You are // responsibile for validating the value. If you do not validate it, // you will experience unexpected results. public static Int32 SHFileOperation(ref SHFILEOPSTRUCT lpFileOp) { MachineType mt = GetMachineType(); Int32 result; switch (mt) { case MachineType.win32: SHFILEOPSTRUCT32 fos32 = new SHFILEOPSTRUCT32(lpFileOp); result = SHFileOperation32(ref fos32); lpFileOp.CopyFrom(fos32); break; case MachineType.win64: SHFILEOPSTRUCT64 fos64 = new SHFILEOPSTRUCT64(lpFileOp); result = SHFileOperation64(ref fos64); lpFileOp.CopyFrom(fos64); break; default: throw new ArgumentException("Hell, what kind of computer are you using? It's not 32 and not 64 bit"); } return result; }
public static extern Int32 SHFileOperation64( ref SHFILEOPSTRUCT64 lpFileOp); // Address of an SHFILEOPSTRUCT structure that contains information
public static extern int SHFileOperation(ref SHFILEOPSTRUCT64 FileOp);
/// <summary> /// This function can be used to copy, move, rename, or delete a file system object. /// </summary> /// <param name="lpFileOp">Address of an SHFILEOPSTRUCT structure that contains information this function needs to carry out the specified operation.</param> /// <returns>Returns true if successful, or false otherwise.</returns> public static int SHFileOperation(SHFILEOPSTRUCT lpFileOp) { if (IntPtr.Size == 4) { return SHFileOperation32(lpFileOp); } var shfileopstruct1 = new SHFILEOPSTRUCT64 { hwnd = lpFileOp.hwnd, wFunc = lpFileOp.wFunc, pFrom = lpFileOp.pFrom, pTo = lpFileOp.pTo, fFlags = lpFileOp.fFlags, fAnyOperationsAborted = lpFileOp.fAnyOperationsAborted, hNameMappings = lpFileOp.hNameMappings, lpszProgressTitle = lpFileOp.lpszProgressTitle }; int ret = SHFileOperation64(shfileopstruct1); lpFileOp.fAnyOperationsAborted = shfileopstruct1.fAnyOperationsAborted; return ret; }
private static extern int SHFileOperation64([In, Out] SHFILEOPSTRUCT64 lpFileOp);
private static extern int SHFileOperation64(ref SHFILEOPSTRUCT64 lpFileOp);
private static void CopyFileSystemEntries(string[] sources, string target) { if (Environment.Is64BitProcess) { SHFILEOPSTRUCT64 copyCommand = new SHFILEOPSTRUCT64(); copyCommand.hwnd = IntPtr.Zero; copyCommand.wFunc = FO_Func.FO_COPY; copyCommand.fFlags = FILEOP_FLAGS.FOF_NO_CONNECTED_ELEMENTS | FILEOP_FLAGS.FOF_ALLOWUNDO; copyCommand.lpszProgressTitle = "CoCoMO - COPYING"; copyCommand.pTo = target + "\0\0"; var sb = new StringBuilder(); foreach (var sourceItem in sources) { sb.Append(sourceItem + "\0"); } sb.Append("\0\0"); copyCommand.pFrom = sb.ToString(); var result = SHFileOperationW64(ref copyCommand); if (result != 0 && result != 0x75 && result != 0x4C7) { //0 is success, others are 'user cancelled' UI.Error("SHFileOperationW64 returned " + result.ToString()); } } else { SHFILEOPSTRUCT32 copyCommand = new SHFILEOPSTRUCT32(); copyCommand.hwnd = IntPtr.Zero; copyCommand.wFunc = FO_Func.FO_COPY; copyCommand.fFlags = FILEOP_FLAGS.FOF_NO_CONNECTED_ELEMENTS | FILEOP_FLAGS.FOF_ALLOWUNDO; copyCommand.lpszProgressTitle = "CoCoMO - COPYING"; copyCommand.pTo = target + "\0\0"; var sb = new StringBuilder(); foreach (var sourceItem in sources) { sb.Append(sourceItem + "\0"); } sb.Append("\0\0"); copyCommand.pFrom = sb.ToString(); var result = SHFileOperationW32(ref copyCommand); if (result != 0 && result != 0x75 && result != 0x4C7) { //0 is success, others are 'user cancelled' UI.Error("SHFileOperationW32 returned " + result.ToString()); } } }