0

i write a snippet code that deletes a specified directory in SHFileOperation method.

The SHFileOperation class from a pinvoke.net

the flowing is my test code:

 var interop = new InteropSHFileOperation();
 interop.wFunc = InteropSHFileOperation.FO_Func.FO_DELETE;
 interop.pFrom = path;
 interop.fFlags.FOF_SILENT = true;
 interop.fFlags.FOF_NOERRORUI = true;
 interop.fFlags.FOF_NOCONFIRMATION = true;
 return interop.Execute();          

the above code can worked in my computer(win7,32-bit,.net 4.0),

but when running above code to my other computer(win 2008,64-bit,.net 4.0),i get the flowing error(from windows event viewer):

Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
   at Shopbots.IO.InteropSHFileOperation.SHFileOperation(SHFILEOPSTRUCT ByRef)
   at Shopbots.IO.InteropSHFileOperation.SHFileOperation(SHFILEOPSTRUCT ByRef)
   at Shopbots.IO.InteropSHFileOperation.Execute()

and from windows exceton dialog

event name : APPCRASH
Fault Module Name:  shell32.dll
Fault Module Version:   6.0.6002.18646
Fault Module Timestamp: 4fd23d65
Exception Code: c0000005

[update 2]

according "Don't declare a value for the Pack size. If you omit it, the correct value is used when marshaling and a single SHFILEOPSTRUCT can be used for both 32-bit and 64-bit operation." from a other article:http://www.pinvoke.net/default.aspx/Structures/SHFILEOPSTRUCT.html:

change a SHFILEOPSTRUCT declare that work 32-bit and 64-bit windows operator system(because the InteropSHFileOperation class from pinvoke site that declare a SHFILEOPSTRUCT structure for 32-bit operator system)

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
struct SHFILEOPSTRUCT
{
  ....
}
7
  • 1
    Did you read the comments about the string needing to be double null terminated? pinvoke.net/default.aspx/shell32.shfileoperation Commented Jul 13, 2013 at 5:45
  • yes,i have read.i just noticed this may be the operating system version 32 and 64 issues.i read again and notice about this :The SHFILEOPSTRUCT StructLayout attribute needs a Pack=2 (win32 - see this in the example) and Pack=8 (win64) parameter. Without it, it does not work. Of course both cannot be set for the same struct, so 2 structs are needed. Commented Jul 13, 2013 at 5:56
  • Don't pinvoke this, it is already wrapped by the .NET framework. Use FileSystem.DeleteFile() instead. Commented Jul 14, 2013 at 19:34
  • 2
    It's a standard Win32 struct. It's aligned. Remove Pack from your declarations. Commented Jul 15, 2013 at 7:12
  • @Hans The asker is trying to delete a directory. Commented Jul 15, 2013 at 7:13

1 Answer 1

1

The most common failure mode for SHFileOperation is that the paths need to be double null-terminated. I suspect that you have forgotten to do that and if you do so an access violation is one possible outcome.

As for packing of the struct, it's a standard Win32 struct. It's not packed, it is aligned. Remove the Pack parameter from the StructLayout attribute.

I cannot understand why you would not call FileSystem.DeleteDirectory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.