QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3222|回复: 0
打印 上一主题 下一主题

Delete or move files while displaying the flying folders

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 13:16 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

Answer: You can easily copy, delete, or move files by calling the API functions CopyFile, DeleteFile, and MoveFile. These functions are pretty easy to use, but they do not display the flying folders. Here are examples of each.

" ~6 f4 L4 A* z' i- p

// copy SOURCE.TXT to DEST.TXT, the FALSE argument tells windows 3 N- r% G- r1 }# E. w& V5 W// to fail if DEST.TXT already exists. CopyFile returns BOOL & ?' {4 v& }6 z) L2 Q% dif( CopyFile("C:\\SOURCE.TXT","C:\\DEST.TXT", FALSE) == FALSE)2 ~) F: t/ S& S9 f Application->MessageBox("CopyFile failed", "Error", MB_OK);

8 c/ k x0 N0 P8 U

// MoveFile and DeleteFile also return FALSE if failure. 7 W$ h6 Z" n5 C+ b. J: x& ]MoveFile("C:\\SOURCE.TXT","C:\\WINDOWS\\TEMP\\DEST.TXT");5 z; H9 S& w( m5 ` DeleteFile("C:\\SOURCE.TXT");

1 n L- r) V' B/ y; }

These functions are easy to use, but unfortunately they don't display the flying folders. In order to get the folders, you must use the SHFileOperation API function. The same examples that are shown above have been duplicated below to use SHFileOperation. Notice that the move and copy commands can specify a destination directory.

4 U- z1 f, F1 U5 t4 t

// make sure to include the file SHELLAPI.H. Its located in the % k. `) {4 b& h+ e. {4 b& f% P6 t// INCLUDE\WIN32 file if you're curious. 9 t: w, E9 a, @% v( H' A* p* o#include <shellapi.h>

! V! e8 O0 @2 ?- g

// copy SOURCE.TXT to DEST.TXT; 8 x) A6 F$ f. Gchar *From = "C:\\SOURCE.TXT\0";/ Z; I6 {/ [# }, \8 y4 }$ z1 Z% v" U char *To = "C:\\DEST.TXT\0"; 8 I4 {0 v, t$ NSHFILEOPSTRUCT op; o: k! s# d' ~- o8 G# c' ]3 P ZeroMemory(&op, sizeof(op));: R- r" k, Y- Q9 B' @& y) y op.hwnd = Handle; // Handle of main form or the application" s9 B% f4 D+ M/ N/ H0 m% ]( B op.wFunc = FO_COPY; ' K. g* R& H: W) x v# `2 M5 wop.pFrom = From; ( B. \" v( J+ {5 u! `! I% n/ ^op.pTo = To; 6 s9 |$ o' a* n ], pop.fFlags= 0; 5 X/ q& I$ S3 W8 }% O; FSHFileOperation( &op);

+ E6 O- @ V) K& X

// move SOURCE.TXT the windows temp directory3 R! Y9 i" |- X" `% }. H) K char *From = "C:\\SOURCE.TXT\0";+ h- z6 O& ?$ p8 V: [, T$ X8 M char *To = "C:\\WINDOWS\\TEMP\0"; 5 [+ D2 N" I8 E8 s7 `4 LSHFILEOPSTRUCT op;9 t& @+ f! V) G" M" t# D( Y% c' m ZeroMemory(&op, sizeof(op)); 6 m; O6 N- B( E' ?& u- ?7 rop.hwnd = Handle;. b# D+ _1 ?- x: {" _" } op.wFunc = FO_MOVE;9 T% {$ b" W9 N op.pFrom = From;$ o; k. e8 Q H! i# d: a! s; j op.pTo = To; 1 `4 W8 f9 v8 d- Z* b- mop.fFlags= 0;, x4 ]" @9 |: w3 a, s SHFileOperation( &op);

7 b: N2 z0 S, |: ]7 |

// send all temp files to the recycle bin / j( ~1 `! a* Echar *File = "C:\\windows\\temp\\*.TMP\0"; ' ~3 W% p* [0 d7 ^ YSHFILEOPSTRUCT op;, ] ^4 N, l) u4 y' E ZeroMemory(&op, sizeof(op)); 5 {3 S7 B4 E$ E' _5 f% C1 Top.hwnd = Handle; * U/ M4 M* ^8 j4 g( M2 g+ x7 m5 yop.wFunc = FO_DELETE; , i% _2 X) u) V K6 }5 k- eop.pFrom = File; # G# p7 a/ _+ [) Eop.fFlags= FOF_ALLOWUNDO;% d/ a" b- n& o: P& B SHFileOperation( &op);

, u7 z, ~4 P7 c+ M3 |

// copy all text files in the root drive to ) I9 M$ B# N o( w// the temp directory. 1 W7 a# L7 M8 F. i7 K6 \1 Schar *From = "C:\\*.TXT\0"; : f/ p1 P/ s: H, l3 d9 zchar *To = "C:\\WINDOWS\\TEMP\0";. e$ V, V9 e' B/ d0 U SHFILEOPSTRUCT op; $ V, N$ y% D2 c) _, nZeroMemory(&op, sizeof(op));6 _' B A# K; S. ?' t; e9 E) c op.hwnd = Handle; 8 _$ J2 a+ @1 s( Q" _% p1 Qop.wFunc = FO_COPY;( B6 G$ L2 S0 q0 B7 N' Q1 N op.pFrom = From; 1 |4 g) B( G: v4 Rop.pTo = To; $ f( ~ _& D, Z+ a$ C" c% s. V, @op.fFlags= 0; * N" x+ \1 L6 p& ~8 c& VSHFileOperation( &op);

2 I; p3 L; O |! l3 L

Notes:

; s! f$ _6 @& U5 ]+ h. W/ X

1: When you specify FO_DELETE, the files will be sent to the recycle bin if fFlags contains the FOF_ALLOWUNDO style. Otherwise they will be deleted.

/ ~" F# D+ s9 i0 Q* D' K

2: The pFrom and pTo structure items are char pointers. They are not arrays. They must point to a string, but the structure doesn't contain any memory space to actually contain the strings. Don't do this:

6 h& D/ `3 d+ \4 n

op.pFrom = "C:\\*.TXT";5 `$ W9 r2 ?" I* Y# \0 v! [$ ]+ ? op.pTo = "C:\\TEMP";

& y6 s7 d0 G9 l6 q9 ~

2 t o4 b7 y( ~( ~3 u2 G3: Notice the extra null terminator (added with the trailing '\0') in the file strings . The MSDN documentation states that the pTo and pFrom strings must be double null terminated.

6 y4 n- K. M9 M8 z) f

4: The strings that pFrom and pTo point to can contain more than one string. Each string should be separated with one null terminator. The double null terminator mentioned above is used to terminate the entire list. Here is an example:

( ~4 ~* V9 _% H0 J! Y2 \( i( r6 X

char *From = "C:\\*.txt\0C:\\*.log\0C:\\*.tmp\0"; + W. {, T. {6 q# c& H' |% X op.pFrom = From;

# w6 B7 Z3 ~) J6 e4 N

// the From declaration would be a little ; X* h F f% A* J0 R/ S // cleaner by utilizing white space , R( P3 A3 |- Z5 B char *From = "C:\\*.txt\0" 9 V, W) ?, c# O' U0 d2 b/ W "C:\\*.log\0" : Q- N4 M1 H' t( \ "C:\\*.tmp\0";

2 r! z0 A) D0 _5 V, i) N7 S

5: When copying or moving files, the FOF_RENAMEONCOLLISION style in the fFlags parameter will prevent the function from overwriting existing files. The shell will create copies that say "Copy of readme.txt" (this is what explorer does).

5 J$ `; y O5 x$ h

6: You can also specify FO_RENAME as the wFunc parameter. The fFlags parameter could contain many other advanced styles. Check the WIN32.HLP file for more info. * e& h4 j- V& m, y

zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-18 10:47 , Processed in 0.398662 second(s), 52 queries .

回顶部