Why not just write a function for this specific task?
public static void DeleteFilesInFolder()
{
using(var dlg = new FolderBrowserDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
var folderPath = dlg.SelectedPath;
var dir = new DirectoryInfo(folderPath);
var files =dir.GetFiles();
foreach (var f in files)
{
try
{
f.Delete();
}
catch (Exception ex) {
//handle this error
}
}
}
}
}
ah ok, just a suggestion..
then have a look at this:
http://www.blackwasp.co.uk/RecycleBin2.aspx
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
public uint wFunc;
public string pFrom;
public string pTo;
public ushort fFlags;
public int fAnyOperationsAborted;
public IntPtr hNameMappings;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszProgressTitle;
}
private const int FO_DELETE = 0x0003;
private const int FOF_SILENT = 0x0004;
private const int FOF_ALLOWUNDO = 0x0040;
private const int FOF_NOCONFIRMATION = 0x0010;
private const int FOF_WANTNUKEWARNING = 0x4000;
[DllImport("Shell32.dll")]
static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp);
used like this to delete all files matching the *.*
pattern in a folder:
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle\*.*" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO;
int result = SHFileOperation(ref operation);