473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 9722
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.