SHFileOperation メソッドで指定したディレクトリを削除するスニペット コードを記述します。
pinvoke.net のSHFileOperationクラス
流れるのは私のテストコードです:
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();
上記のコードは、私のコンピューター (win7、32 ビット、.net 4.0) で動作します。
しかし、上記のコードを他のコンピューター (win 2008、64 ビット、.net 4.0) で実行すると、(Windows イベント ビューアーから) フロー エラーが発生します。
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()
およびWindowsエクセトンダイアログから
event name : APPCRASH
Fault Module Name: shell32.dll
Fault Module Version: 6.0.6002.18646
Fault Module Timestamp: 4fd23d65
Exception Code: c0000005
【アップデート2】
「パックサイズの値を宣言しないでください。省略した場合、マーシャリング時に正しい値が使用され、単一の SHFILEOPSTRUCT を 32 ビットと 64 ビットの両方の操作に使用できます。」別の記事から: http ://www.pinvoke.net/default.aspx/Structures/SHFILEOPSTRUCT.html :
32 ビットおよび 64 ビット Windows オペレーター システムで動作する SHFILEOPSTRUCT 宣言を変更します (32 ビット オペレーター システム用の SHFILEOPSTRUCT 構造を宣言する pinvoke サイトの InteropSHFileOperation クラスのため)。
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
struct SHFILEOPSTRUCT
{
....
}