473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete File / Recycle Bin

In c# code how do I delete a file while ensuring it goes into the recycle
bin for possible recovery ?
Nov 16 '05 #1
8 9726
JezB wrote:
In c# code how do I delete a file while ensuring it goes into the recycle
bin for possible recovery ?

I don't think the framework offers this functionality, but you
could always P-Invoke SHFileOperation in shell32.dll.

Willem van Rumpt
Nov 16 '05 #2
Sounds a bit advanced for me - I'm only a beginner. Any guidelines how to do
that ?

Nov 16 '05 #3
JezB wrote:
Sounds a bit advanced for me - I'm only a beginner. Any guidelines how to do
that ?

I don't have Visual Studio handy here, but it off the top of my head
it would look something like this:

[DllImport("shell32.dll",CharSet = CharSet.Unicode)]
static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOpStruct);

but you need the definition of SHFILEOPSTRUCT as well (and some
constants) take a look at www.pinvoke.net and search for SHFileOperation,
I'm quite sure someone already has done the work for you =)

Willem van Rumpt
Nov 16 '05 #4
This has an example:
http://www.andyloree.com/brain/46-Au...ile-Management

"JezB" <je***********@blueyonder.co.uk> wrote in message
news:e7**************@TK2MSFTNGP11.phx.gbl...
In c# code how do I delete a file while ensuring it goes into the recycle
bin for possible recovery ?

Nov 16 '05 #5
Cannot get the damned thing to work ...grrr

"JezB" <je***********@blueyonder.co.uk> wrote in message
news:e7**************@TK2MSFTNGP11.phx.gbl...
In c# code how do I delete a file while ensuring it goes into the recycle
bin for possible recovery ?

Nov 16 '05 #6
I've taken the source for SHFileOperation from www.PInvoke.net
and it works as expected. The source below works properly:

InteropSHFileOperation fo = new InteropSHFileOperation();
fo.wFunc = InteropSHFileOperation.FO_Func.FO_DELETE;
fo.fFlags.FOF_ALLOWUNDO = true;
fo.pFrom = @"C:\test.txt";
if (fo.Execute())
{
MessageBox.Show("test.txt recycled");
}
else
{
MessageBox.Show("Unable to recycle test.txt");
}

Can you post some code, so I can look at it?

Willem van Rumpt

JezB wrote:
Cannot get the damned thing to work ...grrr

"JezB" <je***********@blueyonder.co.uk> wrote in message
news:e7**************@TK2MSFTNGP11.phx.gbl...
In c# code how do I delete a file while ensuring it goes into the recycle
bin for possible recovery ?


Nov 16 '05 #7
Following Shiva's suggested reference, I have this :-

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace FileHandlerNS
{
public class FileHandler
{
// Contains information that the SHFileOperation function uses to
perform file operations.
[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);
}
}
}

Your example seems much simpler! But where is the definition for
InteropSHFileOperation?

Nov 16 '05 #8
OK I found it on the site, and your code worked - I just needed to add the
NOCONFIRMATION flag - thanks very much.

"JezB" <je***********@blueyonder.co.uk> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...
Following Shiva's suggested reference, I have this :-

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace FileHandlerNS
{
public class FileHandler
{
// Contains information that the SHFileOperation function uses to
perform file operations.
[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);
}
}
}

Your example seems much simpler! But where is the definition for
InteropSHFileOperation?

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1822
by: Silvia | last post by:
Hello I have a listview with images, and when I select one image I can delete the image, bu I produce an error, say "the process cannot access the file D:\imagesDicomitzador\142617379\image1.bmp...
3
4030
by: Paul | last post by:
Is it possible to delete a file on a client side machine using VB/JAVA script? My website allows the user to upload a file to the website and after that I would like to delete the file on their...
4
3763
by: Michael K. | last post by:
Hi there I am having trouble deleting an uploaded file that I have just written to disc. I am using the SaveAs method of a HttpPostedFile to write the file to disc, then straight after I'm...
3
1980
by: facicad | last post by:
I would like to know if as event before delete file exist. My probleme is with FileSystemWatch, it is only Deleted event, and went I want copy for bakup my file, the file do not exist :(
2
2243
by: TOI DAY | last post by:
Hi all, How can I delete the file on the server after the user download it? For example: I have file name "123.txt" on a server, I copy it to "ABC.txt", then allow uer download the...
3
1958
by: Parag Gaikwad | last post by:
Hi, I need to delete files across the network using web client (IE 6.x) in Win 2003 - IIS 6.0 environment. Can someone please suggest an approach I can use to acheive this. Will using FSO do...
5
38940
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows...
1
1625
by: ABC | last post by:
Is there any best solution automatic delete file within the special time? My web application can generate many pdf files for users. Because there are many users, I need to control the total...
4
4070
by: id10t error | last post by:
Hello, I am making a program that will not have an user interaction to delete certian files. I found this line of code. My.Computer.FileSystem.DeleteFile("C:\POLLJP.DWN",...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.