SHFileOperation介绍
SHFileOperation
函数功能描述:文件操作,与 Shell 的动作相同.
函数原型:
#include<shellapi.h> WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);
参数:
typedef struct _SHFILEOPSTRUCT { HWND hwnd; //父窗口句柄 UINT wFunc; //要执行的动作 LPCTSTR pFrom; //源文件路径,可以是多个文件 LPCTSTR pTo; //目标路径,可以是路径或文件名 FILEOP_FLAGS fFlags; //标志,附加选项 BOOL fAnyOperationsAborted; //是否可被中断 LPVOID hNameMappings; //文件映射名字,可在其 它 Shell 函数中使用 LPCTSTR lpszProgressTitle; // 只在 FOF_SIMPLEPROGRESS 时,指定对话框的标题。 } SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;
===================
vb.net
Public Structure SHFILEOPSTRUCT Dim hwnd As IntPtr Dim wFunc As Integer Dim pFrom As String Dim pTo As String Dim fFlags As Short Dim fAnyOperationsAborted As Integer Dim hNameMappings As IntPtr Dim lpszProgressTitle As String End Structure Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
======================
vb:
Type SHFILEOPSTRUCT hWnd As Long wFunc As Long pFrom As String '必须用 pFrom & vbNullChar & vbNullChar pTo As String '同pFrom fFlags As Integer fAnyOperationsAborted As Boolean hNameMappings As Long lpszProgressTitle As String End Type Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
=======================
wFunc 可以为:
/FO_MOVE 0x0001 移动文件
FO_COPY 0x0002 复制文件
FO_DELETE 0x0003 删除文件,只使用 pFrom
FO_RENAME 0x0004 文件重命名
fFlags可以为:
FOF_MULTIDESTFILES 0x0001 //pTo 指定了多个目标文件,而不是单个目录
FOF_CONFIRMMOUSE 0x0002
FOF_SILENT 0x00044 // 不显示一个进度对话框
FOF_RENAMEONCOLLISION 0x0008 // 碰到有抵触的名字时,自动分配前缀
FOF_NOCONFIRMATION 0x0010 // 不对用户显示提示
FOF_WANTMAPPINGHANDLE 0x0020 // 填充 hNameMappings 字段,必须使用 SHFreeNameMappings 释放
FOF_ALLOWUNDO 0x0040 // 允许撤销
FOF_FILESONLY 0x0080 // 使用 *.* 时, 只对文件操作
FOF_SIMPLEPROGRESS 0x0100 // 简单进度条,意味者不显示文件名。
FOF_NOCONFIRMMKDIR 0x0200 // 建新目录时不需要用户确定
FOF_NOERRORUI 0x0400 // 不显示出错用户界面
FOF_NOCOPYSECURITYATTRIBS 0x0800 // 不复制 NT 文件的安全属性
FOF_NORECURSION 0x1000 // 不递归目录
返回值:
函数成功返回 0 ,失败返回非 0 。
例子:
1. 将 C:\Test.txt 拷贝到 D:\
SHFILEOPSTRUCT lpsh; ZeroMemory(&lpsh,sizeof(lpsh)); lpsh.hwnd= HWND_DESKTOP; lpsh.fFlags=FOF_NOCONFIRMATION|FOF_SIMPLEPROGRESS ; lpsh.wFunc=FO_COPY; // FO_MOVE 则是移动 lpsh.pFrom= "C:\Test.txt"; lpsh.pTo = "D:\" if( 0 != SHFileOperation(&lpsh)) { AfxMessageBox("复制文件出错,请检查"); return ; }
2. 删除 D:\Test.txt
SHFILEOPSTRUCT lpsh; ZeroMemory(&lpsh,sizeof(lpsh)); lpsh.hwnd= HWND_DESKTOP; lpsh.fFlags=FOF_NOCONFIRMATION|FOF_SIMPLEPROGRESS ; lpsh.wFunc=FO_DELETE; lpsh.pFrom= "D:\Test.txt"; if( 0 != SHFileOperation(&lpsh)) { AfxMessageBox("删除文件出错,请检查"); return ; }
3.重命名
SHFILEOPSTRUCT lpsh; ZeroMemory(&lpsh,sizeof(lpsh)); lpsh.hwnd= HWND_DESKTOP; lpsh.fFlags=FOF_NOCONFIRMATION|FOF_SIMPLEPROGRESS ; lpsh.wFunc=FO_RENAME; lpsh.pFrom= "D:\Test.txt"; lpsh.pTo = "D:\Test2.txt"; if( 0 != SHFileOperation(&lpsh)) { AfxMessageBox("重命名文件出错!"); return ; }
4.VB
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Public Const FO_COPY = &H2 Public Const FOF_ALLOWUNDO = &H40 Public Sub ShellCopyFile(Source As String, Dest As String) Dim result As Long Dim fileop As SHFILEOPSTRUCT With fileop .hwnd = 0 .wFunc = FO_COPY .pFrom = Source & vbNullChar & vbNullChar .pTo = Dest & vbNullChar & vbNullChar .fFlags = FOF_ALLOWUNDO End With result = SHFileOperation(fileop) If result <> 0 Then 'Msgbox the error that occurred in the API. MsgBox Err.LastDllError, vbCritical Or vbOKOnly Else If fileop.fAnyOperationsAborted <> 0 Then MsgBox "Operation Failed", vbCritical Or vbOKOnly End If End Sub