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