1 public static class FileEx
2 {
3 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
4
5 public struct SHFILEOPSTRUCT
6 {
7
8 public IntPtr hwnd;
9
10 [MarshalAs(UnmanagedType.U4)]
11 public int wFunc;
12
13 public string pFrom;
14
15 public string pTo;
16
17 public short fFlags;
18
19 [MarshalAs(UnmanagedType.Bool)]
20 public bool fAnyOperationsAborted;
21
22 public IntPtr hNameMappings;
23
24 public string lpszProgressTitle;
25
26 }
27
28 [DllImport("shell32.dll", CharSet = CharSet.Auto)]
29
30 static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
31
32 const int FO_RENAME = 4;
33
34 const int FO_DELETE = 3;
35
36 const int FO_COPY = 2;
37
38 const int FO_MOVE = 1;
39
40 const int FOF_ALLOWUNDO = 0x40;
41
42 const int FOF_NOCONFIRMATION = 0x10; //Don't prompt the user.;
43
44 const int FOF_SIMPLEPROGRESS = 0x100;
45
46 public static void SendToRecyclyBin(string path)
47 {
48
49 SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
50
51 shf.wFunc = FO_DELETE;
52
53 shf.fFlags = FOF_ALLOWUNDO;// | FOF_NOCONFIRMATION;
54
55 shf.pFrom = path;
56
57 //返回0表示操作成功
58 SHFileOperation(ref shf);
59
60 }
61
62 public static bool Copy(string from, string to)
63 {
64 SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
65
66 shf.wFunc = FO_COPY;
67
68 shf.fFlags = FOF_ALLOWUNDO;
69
70 shf.pFrom = from;
71
72 shf.pTo = to;
73
74 return SHFileOperation(ref shf) == 0;
75
76 }
77
78 public static bool Move(string from, string to)
79 {
80
81 SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
82
83 shf.wFunc = FO_MOVE;
84
85 shf.fFlags = FOF_ALLOWUNDO;
86
87 shf.pFrom = from;
88
89 shf.pTo = to;
90
91 return SHFileOperation(ref shf) == 0;
92
93 }
94
95 public static bool RENAME(string from, string to)
96 {
97
98 SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
99
100 shf.wFunc = FO_RENAME;
101
102 shf.fFlags = FOF_ALLOWUNDO;
103
104 shf.pFrom = from;
105
106 shf.pTo = to;
107
108 return SHFileOperation(ref shf) == 0;
109
110 }
111 }