public class ShellProvider
{
#region imports
[DllImport("shell32.dll")]
static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp);
#endregion
#region nested
private enum FO_Func : uint
{
FO_MOVE = 0x0001,
FO_COPY = 0x0002,
FO_DELETE = 0x0003,
FO_RENAME = 0x0004,
}
private struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
public Int32 wFunc;
[MarshalAs(UnmanagedType.LPWStr)]
public string pFROM;
[MarshalAs(UnmanagedType.LPWStr)]
public string pTo;
public FILEOP_FLAGS_ENUM fFlags;
public bool fAnyOperationsAborted;
public Int32 hNameMappings;
public string lpszProgressTitle;
}
private enum FILEOP_FLAGS_ENUM : ushort
{
FOF_MULTIDESTFILES = 0x0001,
FOF_CONFIRMMOUSE = 0x0002,
FOF_SILENT = 0x0004,
FOF_RENAMEONCOLLISION = 0x0008,
FOF_NOCONFIRMATION = 0x0010,
FOF_WANTMAPPINGHANDLE = 0x0020,
FOF_ALLOWUNDO = 0x0040,
FOF_FILESONLY = 0x0080,
FOF_SIMPLEPROGRESS = 0x0100,
FOF_NOCONFIRMMKDIR = 0x0200,
FOF_NOERRORUI = 0x0400,
FOF_NOCOPYSECURITYATTRIBS = 0x0800,
FOF_NORECURSION = 0x1000,
FOF_NO_CONNECTED_ELEMENTS = 0x2000,
FOF_WANTNUKEWARNING = 0x4000,
FOF_NORECURSEREPARSE = 0x8000,
}
#endregion
#region Fields
#endregion
#region Properties
#endregion
#region Construction / Destruction
#endregion
#region Methods
public void Delete(string sPath, bool bMoveToBin, bool bWithDialog)
{
this.RunFileOperation(sPath, string.Empty, FO_Func.FO_DELETE, (bMoveToBin ? FILEOP_FLAGS_ENUM.FOF_ALLOWUNDO : 0) | (!bWithDialog ? FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION: 0));
}
public void Move(string sSource, string sDestination, bool bWithDialog)
{
this.RunFileOperation(sSource, sDestination, FO_Func.FO_MOVE, !bWithDialog ? FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION : 0);
}
public void Copy(string sSource, string sDestination, bool bWithDialog)
{
this.RunFileOperation(sSource, sDestination, FO_Func.FO_COPY, !bWithDialog ? FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION : 0);
}
public void Rename(string sSource, string sDestination, bool bWithDialog)
{
this.RunFileOperation(sSource, sDestination, FO_Func.FO_RENAME, !bWithDialog ? FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION : 0);
}
private int RunFileOperation(string sSource, string sDestination, FO_Func func, FILEOP_FLAGS_ENUM flags)
{
SHFILEOPSTRUCT structure = new SHFILEOPSTRUCT();
structure.hwnd = IntPtr.Zero;
structure.wFunc = (int)func;
structure.pFROM = sSource;
structure.pTo = sDestination;
structure.fFlags = flags;
return SHFileOperation(ref structure);
}
#endregion
}
|