Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than shell32, prefix the name with the module name and a period.
SHFileOperation (shell32)
.
C# Signature:
[DllImport("shell32.dll",CharSet = CharSet.Unicode)]
static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp);
VB.NET Signature:
<DllImport("shell32.dll", SetLastError:=true, CharSet:=CharSet.Unicode)> _
Public Function SHFileOperation(<MarshalAs(UnmanagedType.Struct)>ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
End function
VB Signature
Public Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Do not use GetLastError with the return values of this function.
Tips & Tricks:
I tried to use this to send files to the recycle bin. It failed (Marshal.GetLastWin32Error was 6, invalid handle) and sometimes threw a NullReferenceException. To make it work I did the following;
The SHFILEOPSTRUCT StructLayout attribute needs a Pack=2 (win32 - see this in the example) and Pack=8 (win64) parameter. Without it, it does not work. Of course both cannot be set for the same struct, so 2 structs are needed.
Sample Code:
using System.Runtime.InteropServices;
public class InteropSHFileOperation
{
public enum FO_Func : uint
{
FO_MOVE = 0x0001,
FO_COPY = 0x0002,
FO_DELETE = 0x0003,
FO_RENAME = 0x0004,
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode, Pack=2)]
struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
public FO_Func wFunc;
[MarshalAs(UnmanagedType.LPWStr)]
public string pFrom;
[MarshalAs(UnmanagedType.LPWStr)]
public string pTo;
public ushort fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszProgressTitle;
private SHFILEOPSTRUCT _ShFile;
public FILEOP_FLAGS fFlags;
public IntPtr hwnd
{
set
{
this._ShFile.hwnd = value;
}
}
public FO_Func wFunc
{
set
{
this._ShFile.wFunc = value;
}
}
public string pFrom
{
set
{
this._ShFile.pFrom = value + '\0' + '\0';
}
}
public string pTo
{
set
{
this._ShFile.pTo = value + '\0' + '\0';
}
}
public bool fAnyOperationsAborted
{
set
{
this._ShFile.fAnyOperationsAborted = value;
}
}
public IntPtr hNameMappings
{
set
{
this._ShFile.hNameMappings = value;
}
}
public string lpszProgressTitle
{
set
{
this._ShFile.lpszProgressTitle = value + '\0';
}
}
public bool Execute()
{
this._ShFile.fFlags = this.fFlags.Flag;
int ReturnValue = SHFileOperation(ref this._ShFile);
if ( ReturnValue == 0 )
{
return true;
}
else
{
return false;
}
}
public class FILEOP_FLAGS
{
[Flags]
private enum FILEOP_FLAGS_ENUM : ushort
{
FOF_MULTIDESTFILES = 0x0001,
FOF_CONFIRMMOUSE = 0x0002,
FOF_SILENT = 0x0004, // don't create progress/report
FOF_RENAMEONCOLLISION = 0x0008,
FOF_NOCONFIRMATION = 0x0010, // Don't prompt the user.
FOF_WANTMAPPINGHANDLE = 0x0020, // Fill in SHFILEOPSTRUCT.hNameMappings
// Must be freed using SHFreeNameMappings
FOF_ALLOWUNDO = 0x0040,
FOF_FILESONLY = 0x0080, // on *.*, do only files
FOF_SIMPLEPROGRESS = 0x0100, // means don't show names of files
FOF_NOCONFIRMMKDIR = 0x0200, // don't confirm making any needed dirs
FOF_NOERRORUI = 0x0400, // don't put up error UI
FOF_NOCOPYSECURITYATTRIBS = 0x0800, // dont copy NT file Security Attributes
FOF_NORECURSION = 0x1000, // don't recurse into directories.
FOF_NO_CONNECTED_ELEMENTS = 0x2000, // don't operate on connected elements.
FOF_WANTNUKEWARNING = 0x4000, // during delete operation, warn if nuking instead of recycling (partially overrides FOF_NOCONFIRMATION)
FOF_NORECURSEREPARSE = 0x8000, // treat reparse points as objects, not containers
}
public bool FOF_MULTIDESTFILES = false;
public bool FOF_CONFIRMMOUSE = false;
public bool FOF_SILENT = false;
public bool FOF_RENAMEONCOLLISION = false;
public bool FOF_NOCONFIRMATION = false;
public bool FOF_WANTMAPPINGHANDLE = false;
public bool FOF_ALLOWUNDO = false;
public bool FOF_FILESONLY = false;
public bool FOF_SIMPLEPROGRESS = false;
public bool FOF_NOCONFIRMMKDIR = false;
public bool FOF_NOERRORUI = false;
public bool FOF_NOCOPYSECURITYATTRIBS = false;
public bool FOF_NORECURSION = false;
public bool FOF_NO_CONNECTED_ELEMENTS = false;
public bool FOF_WANTNUKEWARNING = false;
public bool FOF_NORECURSEREPARSE = false;
public ushort Flag
{
get
{
ushort ReturnValue = 0;
if ( this.FOF_MULTIDESTFILES == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_MULTIDESTFILES;
if ( this.FOF_CONFIRMMOUSE == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_CONFIRMMOUSE;
if ( this.FOF_SILENT == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_SILENT;
if ( this.FOF_RENAMEONCOLLISION == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_RENAMEONCOLLISION;
if ( this.FOF_NOCONFIRMATION == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION;
if ( this.FOF_WANTMAPPINGHANDLE == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_WANTMAPPINGHANDLE;
if ( this.FOF_ALLOWUNDO == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_ALLOWUNDO;
if ( this.FOF_FILESONLY == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_FILESONLY;
if ( this.FOF_SIMPLEPROGRESS == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_SIMPLEPROGRESS;
if ( this.FOF_NOCONFIRMMKDIR == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCONFIRMMKDIR;
if ( this.FOF_NOERRORUI == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOERRORUI;
if ( this.FOF_NOCOPYSECURITYATTRIBS == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCOPYSECURITYATTRIBS;
if ( this.FOF_NORECURSION == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NORECURSION;
if ( this.FOF_NO_CONNECTED_ELEMENTS == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NO_CONNECTED_ELEMENTS;
if ( this.FOF_WANTNUKEWARNING == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_WANTNUKEWARNING;
if ( this.FOF_NORECURSEREPARSE == true )
ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NORECURSEREPARSE;
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).