For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2005 > How to send a file or folder to the recycle bin?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to send a file or folder to the recycle bin?
Mariano

2005-01-31, 3:59 pm

Hi, i searched the groups on this topic, but the sample code i have
found didnt work, and i dont know why, anybody can help me with this,
thanks!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;

namespace WindowsApplication2{
public class FileHandler {
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct SHFILEOPSTRUCT {
public IntPtr hwnd;
public UInt32 wFunc;
public string pFrom;
public string pTo;
public UInt16 fFlags;
public Int32 fAnyOperationsAborted;
public IntPtr hNameMappings;
[MarshalAs(UnmanagedType.LPWStr)]
public String lpszProgressTitle;
}

[DllImport("shell32.dll" , CharSet = CharSet.Unicode)]
public static extern Int32 SHFileOperation(ref SHFILEOPSTRUCT
lpFileOp);

const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x10;

public static void DeleteFile(string fname) {
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.hwnd = IntPtr.Zero;
shf.wFunc = (uint) FO_DELETE;
shf.fFlags = (ushort) FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.pTo = fname;
shf.fAnyOperationsAborted = 0;
shf.hNameMappings = IntPtr.Zero;
SHFileOperation(ref shf);
}
}

public class Form1 : System.Windows.Forms.Form{
private System.ComponentModel.Container components = null;
public Form1(){
InitializeComponent();
}

protected override void Dispose( bool disposing ) {
if( disposing ) {
if (components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent(){
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

[STAThread]
static void Main() {
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e) {
FileHandler.DeleteFile(@"c:\temp\test.mp3");
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct SHFILEOPSTRUCT {
public IntPtr hwnd;
public UInt32 wFunc;
public IntPtr pFrom;
public IntPtr pTo;
public UInt16 fFlags;
public Int32 fAnyOperationsAborted;
public IntPtr hNameMappings;
[MarshalAs(UnmanagedType.LPWStr)]
public String lpszProgressTitle;
}

[DllImport("shell32.dll" , CharSet = CharSet.Unicode)]
public static extern Int32 SHFileOperation(ref SHFILEOPSTRUCT
lpFileOp);
const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x10;

public static void Recycle(string sPath) {
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.hwnd = IntPtr.Zero;
shf.wFunc = (uint) FO_DELETE;
shf.fFlags = (ushort) FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.pTo = Marshal.StringToHGlobalUni(sPath);
shf.fAnyOperationsAborted = 0;
shf.hNameMappings = IntPtr.Zero;
SHFileOperation(ref shf);
}
}
}

i cant get the file to the recycle bin with this code, i get no
errors.
jdmartinez@ea.com

2005-01-31, 8:58 pm

Perhaps it's a permissions thing? did you try deleting something on
your desktop (where you are sure to have access to it)?

Joel Martinez
http://www.onetug.org - Orlando .NET User Group
http://www.codecube.net - blog

jdmartinez@ea.com

2005-01-31, 8:58 pm

Perhaps it's a permissions thing? did you try deleting something on
your desktop (where you are sure to have access to it)?

Joel Martinez
http://www.onetug.org - Orlando .NET User Group
http://www.codecube.net - blog

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com