473,395 Members | 1,956 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,395 software developers and data experts.

Help with SHFILEOPSTRUCT + file deletion

Hi, I cannot get the code below to work (C#). I'm trying to send a
file to the recycling bin, and I keep getting the following error:

"Cannot delete file: Cannot read from the source file or disk."

Most tips i've found online talk about terminating the path with null
chars, but I'm already doing this:

SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.pFrom = this.LocalDirectoryName + "\\" + _filename + '\0' + '\0';
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
SHFileOperation(ref shf);

I know the file and path are correct...File.Delete works on the same
file (however doesn't go to recycling bin).

What am I doing wrong?? Thanks in advance.

here is some more relevant code:

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")]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x0010; //Don't prompt the user.;

Nov 17 '05 #1
2 4247
"Stefan" <st****@dragolov.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi, I cannot get the code below to work (C#). I'm trying to send a
file to the recycling bin, and I keep getting the following error:

"Cannot delete file: Cannot read from the source file or disk."

Most tips i've found online talk about terminating the path with null
chars, but I'm already doing this:

SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.pFrom = this.LocalDirectoryName + "\\" + _filename + '\0' + '\0';
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
SHFileOperation(ref shf);

I know the file and path are correct...File.Delete works on the same
file (however doesn't go to recycling bin).

What am I doing wrong?? Thanks in advance.

here is some more relevant code:

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")]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x0010; //Don't prompt the user.;


I used SHFILEOPSTRUCT a while back to "delete" to the recycling bin. I used
the following for SHFILEOPSTRUCT and had no problems:

struct SHFILEOPSTRUCT

{

public IntPtr hwnd;

public FO_Func wFunc;

[MarshalAs(UnmanagedType.LPWStr)]

public string pFrom;

[MarshalAs(UnmanagedType.LPWStr)]

public string pTo;

public ushort fFlags;

public bool fAnyOperationsAborted;

public IntPtr hNameMappings;

[MarshalAs(UnmanagedType.LPWStr)]

public string lpszProgressTitle;

}

Also, for the operations constants, I used the following:

const uint FO_MOVE = 0x0001,

const uint FO_COPY = 0x0002,

const uint FO_DELETE = 0x0003,

const uint FO_RENAME = 0x0004

Not sure if these differences are what is causing your problem but it may
be.

CVD
Nov 17 '05 #2
Ah, I solved the problem...

I replaced this:
[DllImport("shell32.dll")]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

with this:
[DllImport("shell32.dll", CharSet=CharSet.Unicode)]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

Everything else remained the same as the code I provided earlier.
Thanks.

Cletus Van Damme wrote:
"Stefan" <st****@dragolov.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi, I cannot get the code below to work (C#). I'm trying to send a
file to the recycling bin, and I keep getting the following error:

"Cannot delete file: Cannot read from the source file or disk."

Most tips i've found online talk about terminating the path with null
chars, but I'm already doing this:

SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.pFrom = this.LocalDirectoryName + "\\" + _filename + '\0' + '\0';
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
SHFileOperation(ref shf);

I know the file and path are correct...File.Delete works on the same
file (however doesn't go to recycling bin).

What am I doing wrong?? Thanks in advance.

here is some more relevant code:

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")]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x0010; //Don't prompt the user.;


I used SHFILEOPSTRUCT a while back to "delete" to the recycling bin. I used
the following for SHFILEOPSTRUCT and had no problems:

struct SHFILEOPSTRUCT

{

public IntPtr hwnd;

public FO_Func wFunc;

[MarshalAs(UnmanagedType.LPWStr)]

public string pFrom;

[MarshalAs(UnmanagedType.LPWStr)]

public string pTo;

public ushort fFlags;

public bool fAnyOperationsAborted;

public IntPtr hNameMappings;

[MarshalAs(UnmanagedType.LPWStr)]

public string lpszProgressTitle;

}

Also, for the operations constants, I used the following:

const uint FO_MOVE = 0x0001,

const uint FO_COPY = 0x0002,

const uint FO_DELETE = 0x0003,

const uint FO_RENAME = 0x0004

Not sure if these differences are what is causing your problem but it may
be.

CVD


Nov 17 '05 #3

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

Similar topics

2
by: Justin | last post by:
Hi all, Does anybody know a way, in VB.Net, I can discover the file locks on a file on the local machine. I have an application which needs to delete specific files, but I would like to verify...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
14
by: Just Me | last post by:
Can anyone fix the code below? I need to set pTo to NULL and pFrom to a fullfilepath followed by two NULLs Below Filename is a string With FileOperation ..wFunc = FO_DELETE ..pFrom =...
4
by: gsb58 | last post by:
Hi! I've been following the example on SHFILEOPSTRUCT here in the group, but I keep getting the message: 'Cannot read from sourcefile'. Here's the code: SHFILEOPSTRUCT shf = new...
3
by: A_Republican | last post by:
I am interested in writing my own secure file deletion program. I want to be able to read and write to my hard drive directly. My application will seach my hard drive for all locations marked for...
7
by: Sanket80 | last post by:
Hi I need one small help in VB I have written a macro which produces some output and stores them in the form of an excel files (about 10 files are generated) in a particular directory in C:\....
0
by: ferdz3001 | last post by:
Hi, I have read some of the forums regarding deletion of log files. Just to get more information before I perform the deletion of log files. These are the steps I got: 1. db2 get db cfg <DBSID>...
4
by: BibhuAshish | last post by:
Hi, I wanted to delete a line from xml file which i did it. But after deletion of that line there is a blank space. Again if i am adding another line by using java that blank line remains as...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.