Tim

Tim

  • NA
  • 1
  • 0

C# Deleting Files to the Recycle Bin Question

Jul 4 2006 3:36 PM
I'm attemping to delete files to the Recycle Bin using the following code and that part seems to work. What I'd like to do is once the "Delete items to Recycle Bin" popups up I'd like to have it keep focus until the user selects OK or Cancel. Right now if you select the parent Form while the popup is up it just drops behind the parent form losing focus. If I can't do this with this code I guess I'll just make my own form popup that looks like the Recycle Bin Delete one and then use this code with no confirmation turned on while disabling the parent form and setting the new form to topmost. Thanks in advance. 

private const int FO_DELETE          = 3;
private const int FOF_ALLOWUNDO      = 0x40;
private const int FOF_NOCONFIRMATION = 0x0010;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct SHFILEOPSTRUCT
{
	public IntPtr hwnd;
	[MarshalAs(UnmanagedType.U4)] public int wFunc;
	public string pFrom;
	public string pTo;
	public short fFlags;
	[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
	public IntPtr hNameMappings;
	public string lpszProgressTitle;
}

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

...

SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
fileop.wFunc = FO_DELETE;
fileop.pFrom = filePath + '\0' + '\0';
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;

SHFileOperation(ref fileop);