Help with SHFILEOPSTRUCT + file deletion

S

Stefan

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.;
 
C

Cletus Van Damme

Stefan said:
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
 
S

Stefan

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.


Stefan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top