472,146 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Another problem with SHFILEOPSTRUCT

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

Nov 25 '05 #1
4 3199
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 25 '05 #2
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

Nov 25 '05 #3
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 25 '05 #4
Thank you!

Now it works....

Me.Name

Nov 28 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Viviana Vc | last post: by
2 posts views Thread by terence.parker | last post: by
2 posts views Thread by Stefan | last post: by
14 posts views Thread by Just Me | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

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.