Another problem with SHFILEOPSTRUCT

G

gsb58

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 SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.pFrom = myFile;

///'myFile' keeps the filelocation

shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
SHFileOperation(ref shf);

And my question is:

Do you think I have put the declaration code in the wrong place or...?

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", CharSet=CharSet.Unicode)]
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 have put this code just after the 'static void Main()' procedure.

Please guide me!

Me.Name
 
M

Mattias Sjögren

I've been following the example on SHFILEOPSTRUCT here in the group,
but I keep getting the message: 'Cannot read from sourcefile'.

You're calling the Unicode version of the function with an ANSI
version of SHFILEOPSTRUCT. Make sure you keep the CharSet of the
function and the struct in sync.


Mattias
 
G

gsb58

Thanks,

I've tried all the examples given in the group, but keep getting the
message:

'Cannot delete file: Cannot read from the source file or disk'

I changed the code to this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
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", CharSet=CharSet.Auto)]
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.;

Strange thing, eh?

Me.Name
 
M

Mattias Sjögren

I've tried all the examples given in the group, but keep getting the
message:

'Cannot delete file: Cannot read from the source file or disk'

You must also add an extra null character to terminate the source
paths (since you're allowed to provide more than one path).

shf.pFrom = myFile + "\0";


Mattias
 

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