SHFileOperation函數功能描述:文件操作,與 Shell 的動(dòng)作相同.函數原型:#include<shellapi.h>WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);參數:typedef struct _SHFILEOPSTRUCT{ HWND hwnd; //父窗口句柄 UINT wFunc; //要執行的動(dòng)作 LPCTSTR pFrom; //源文件路徑,可以是多個(gè)文件 LPCTSTR pTo; //目標路徑,可以是路徑或文件名 FILEOP_FLAGS fFlags; //標志,附加選項 BOOL fAnyOperationsAborted; //是否可被中斷 LPVOID hNameMappings; //文件映射名字,可在其它 Shell 函數中使用 LPCTSTR lpszProgressTitle; // 只在 FOF_SIMPLEPROGRESS 時(shí),指定對話(huà)框的標題。} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;===================vb.netPublic Structure SHFILEOPSTRUCTDim hwnd As IntPtrDim wFunc As IntegerDim pFrom As StringDim pTo As StringDim fFlags As ShortDim fAnyOperationsAborted As IntegerDim hNameMappings As IntPtrDim lpszProgressTitle As StringEnd StructurePublic Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer======================vb:Type SHFILEOPSTRUCThWnd As LongwFunc As LongpFrom As String '必須用 pFrom & vbNullChar & vbNullCharpTo As String '同pFromfFlags As IntegerfAnyOperationsAborted As BooleanhNameMappings As LonglpszProgressTitle As StringEnd TypePublic Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long=======================wFunc 可以為:/FO_MOVE 0x0001 移動(dòng)文件FO_COPY 0x0002 復制文件FO_DELETE 0x0003 刪除文件,只使用 pFromFO_RENAME 0x0004 文件重命名fFlags可以為:FOF_MULTIDESTFILES 0x0001 //pTo 指定了多個(gè)目標文件,而不是單個(gè)目錄FOF_CONFIRMMOUSE 0x0002FOF_SILENT 0x00044 // 不顯示一個(gè)進(jìn)度對話(huà)框FOF_RENAMEONCOLLISION 0x0008 // 碰到有抵觸的名字時(shí),自動(dòng)分配前綴FOF_NOCONFIRMATION 0x0010 // 不對用戶(hù)顯示提示FOF_WANTMAPPINGHANDLE 0x0020 // 填充 hNameMappings 字段,必須使用 SHFreeNameMappings 釋放FOF_ALLOWUNDO 0x0040 // 允許撤銷(xiāo)FOF_FILESONLY 0x0080 // 使用 *.* 時(shí), 只對文件操作FOF_SIMPLEPROGRESS 0x0100 // 簡(jiǎn)單進(jìn)度條,意味者不顯示文件名。FOF_NOCONFIRMMKDIR 0x0200 // 建新目錄時(shí)不需要用戶(hù)確定FOF_NOERRORUI 0x0400 // 不顯示出錯用戶(hù)界面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 則是移動(dòng) 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.VBPublic Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongPublic Const FO_COPY = &H2Public Const FOF_ALLOWUNDO = &H40Public Sub ShellCopyFile(Source As String, Dest As String)Dim result As LongDim fileop As SHFILEOPSTRUCTWith fileop .hwnd = 0 .wFunc = FO_COPY .pFrom = Source & vbNullChar & vbNullChar .pTo = Dest & vbNullChar & vbNullChar .fFlags = FOF_ALLOWUNDOEnd Withresult = SHFileOperation(fileop)If result <> 0 Then'Msgbox the error that occurred in the API. MsgBox Err.LastDllError, vbCritical Or vbOKOnlyElse If fileop.fAnyOperationsAborted <> 0 Then MsgBox "Operation Failed", vbCritical Or vbOKOnly End IfEnd IfEnd Sub
|
聯(lián)系客服