1
public class FileDelete
2
{
3
private const int FO_DELETE = 0x3;
4
private const ushort FOF_NOCONFIRMATION = 0x10;
5
private const ushort FOF_ALLOWUNDO = 0x40;
6
7
[DllImport("shell32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
8
private static extern int SHFileOperation([In,Out] _SHFILEOPSTRUCT str);
9
10
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
11
private class _SHFILEOPSTRUCT
12
{
13
public IntPtr hwnd;
14
public UInt32 wFunc;
15
public string pFrom;
16
public string pTo;
17
public UInt16 fFlags;
18
public Int32 fAnyOperationsAborted;
19
public IntPtr hNameMappings;
20
public string lpszProgressTitle;
21
}
22
public static bool Delete(string path)
23
{
24
_SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
25
pm.wFunc = FO_DELETE;
26
pm.pFrom = path + '\0';
27
pm.pTo = null;
28
pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
29
int i = SHFileOperation(pm);
30
if(i == 0)
31
return true;
32
return false;
33
}
34
35
}
public class FileDelete2
{3
private const int FO_DELETE = 0x3; 4
private const ushort FOF_NOCONFIRMATION = 0x10; 5
private const ushort FOF_ALLOWUNDO = 0x40; 6
7
[DllImport("shell32.dll", SetLastError=true, CharSet=CharSet.Unicode)] 8
private static extern int SHFileOperation([In,Out] _SHFILEOPSTRUCT str); 9
10
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] 11
private class _SHFILEOPSTRUCT 12
{ 13
public IntPtr hwnd; 14
public UInt32 wFunc; 15
public string pFrom; 16
public string pTo; 17
public UInt16 fFlags; 18
public Int32 fAnyOperationsAborted; 19
public IntPtr hNameMappings; 20
public string lpszProgressTitle; 21
} 22
public static bool Delete(string path) 23
{ 24
_SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT(); 25
pm.wFunc = FO_DELETE; 26
pm.pFrom = path + '\0'; 27
pm.pTo = null; 28
pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; 29
int i = SHFileOperation(pm); 30
if(i == 0)31
return true;32
return false;33
} 34

35
}

浙公网安备 33010602011771号