- SHFileOperation
-
- 函數(shù)功能描述:文件操作,與 Shell 的動(dòng)作相同.
- 函數(shù)原型:
- #include<shellapi.h>
-
- WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);
-
-
-
- 參數(shù):
-
- typedef struct _SHFILEOPSTRUCT
-
- {
-
- HWND hwnd;
-
- UINT wFunc;
-
- LPCTSTR pFrom;
-
- LPCTSTR pTo;
-
- FILEOP_FLAGS fFlags;
-
- BOOL fAnyOperationsAborted;
-
- LPVOID hNameMappings;
-
- LPCTSTR lpszProgressTitle;
-
- } SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;
-
- =======================
-
- wFunc 可以為:
-
- /FO_MOVE 0x0001 移動(dòng)文件
-
- FO_COPY 0x0002 復(fù)制文件
-
- FO_DELETE 0x0003 刪除文件,只使用 pFrom
-
- FO_RENAME 0x0004 文件重命名
-
-
-
- fFlags可以為:
-
- FOF_MULTIDESTFILES 0x0001
-
- FOF_CONFIRMMOUSE 0x0002
-
- FOF_SILENT 0x00044
-
- FOF_RENAMEONCOLLISION 0x0008
-
- FOF_NOCONFIRMATION 0x0010
-
- FOF_WANTMAPPINGHANDLE 0x0020
-
- FOF_ALLOWUNDO 0x0040
-
- FOF_FILESONLY 0x0080
-
- FOF_SIMPLEPROGRESS 0x0100
-
- FOF_NOCONFIRMMKDIR 0x0200
-
- FOF_NOERRORUI 0x0400
-
- FOF_NOCOPYSECURITYATTRIBS 0x0800
-
- FOF_NORECURSION 0x1000
-
-
-
- 返回值:
-
- 函數(shù)成功返回 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;
-
- lpsh.pFrom= "C:\Test.txt";
-
- lpsh.pTo = "D:\"
-
- if( 0 != SHFileOperation(&lpsh))
-
- {
-
- AfxMessageBox("復(fù)制文件出錯(cuò),請(qǐng)檢查");
-
- 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("刪除文件出錯(cuò),請(qǐng)檢查");
-
- 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("重命名文件出錯(cuò)!");
-
- return ;
-
- }
-
- 注意事項(xiàng):
- 參數(shù)pTo、pFrom必須是以 雙0 結(jié)束的,原文為:
- pFrom
- Pointer to a buffer that specifies one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated.
- a.如果是直接賦值:
- WCHAR strSrc[MAX_PATH] = _T("g:\\123");
WCHAR strDst[MAX_PATH] = _T("g:\\456"); - 這樣就可以成功編譯。
-
- b.如果pTo、pFrom從CString獲得,則:
- WCHAR SrcFolder[MAX_PATH],DestFolder[MAX_PATH];
- wcscpy_s(SrcFolder, Source);
wcscpy_s(DestFolder,m_Path);
SrcFolder[wcslen(SrcFolder) + 1] = 0;
DestFolder[wcslen(DestFolder) + 1] = 0;(其中,Source與m_Path是在其他地方獲取的值) - 造成這種差異的原因是:CString在結(jié)尾并沒有"\0",所以需要我們添加。
-
-
- 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 If
-
- End Sub
SHFileOperation函數(shù)功能描述:文件操作,與 Shell 的動(dòng)作相同.函數(shù)原型:#include<shellapi.h>WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);參數(shù):typedef struct _SHFILEOPSTRUCT{ HWND hwnd; //父窗口句柄 UINT wFunc; //要執(zhí)行的動(dòng)作 LPCTSTR pFrom; //源文件路徑,可以是多個(gè)文件 LPCTSTR pTo; //目標(biāo)路徑,可以是路徑或文件名 FILEOP_FLAGS fFlags; //標(biāo)志,附加選項(xiàng) BOOL fAnyOperationsAborted; //是否可被中斷 LPVOID hNameMappings; //文件映射名字,可在其它 Shell 函數(shù)中使用 LPCTSTR lpszProgressTitle; // 只在 FOF_SIMPLEPROGRESS 時(shí),指定對(duì)話框的標(biāo)題。} 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 復(fù)制文件FO_DELETE 0x0003 刪除文件,只使用 pFromFO_RENAME 0x0004 文件重命名fFlags可以為:FOF_MULTIDESTFILES 0x0001 //pTo 指定了多個(gè)目標(biāo)文件,而不是單個(gè)目錄FOF_CONFIRMMOUSE 0x0002FOF_SILENT 0x00044 // 不顯示一個(gè)進(jìn)度對(duì)話框FOF_RENAMEONCOLLISION 0x0008 // 碰到有抵觸的名字時(shí),自動(dòng)分配前綴FOF_NOCONFIRMATION 0x0010 // 不對(duì)用戶顯示提示FOF_WANTMAPPINGHANDLE 0x0020 // 填充 hNameMappings 字段,必須使用 SHFreeNameMappings 釋放FOF_ALLOWUNDO 0x0040 // 允許撤銷FOF_FILESONLY 0x0080 // 使用 *.* 時(shí), 只對(duì)文件操作FOF_SIMPLEPROGRESS 0x0100 // 簡(jiǎn)單進(jìn)度條,意味者不顯示文件名。FOF_NOCONFIRMMKDIR 0x0200 // 建新目錄時(shí)不需要用戶確定FOF_NOERRORUI 0x0400 // 不顯示出錯(cuò)用戶界面FOF_NOCOPYSECURITYATTRIBS 0x0800 // 不復(fù)制 NT 文件的安全屬性FOF_NORECURSION 0x1000 // 不遞歸目錄返回值:函數(shù)成功返回 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("復(fù)制文件出錯(cuò),請(qǐng)檢查"); 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("刪除文件出錯(cuò),請(qǐng)檢查"); 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("重命名文件出錯(cuò)!"); 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
- 單開一個(gè)內(nèi)存,清空,把pForm指向這里,把內(nèi)容拷進(jìn)去
-
-
-
- SHFILEOPSTRUCT Op;
-
- ZeroMemory(&Op, sizeof(Op));
-
-
-
- TCHAR ToBuf[MAX_PATH];
-
- TCHAR FromBuf[MAX_PATH];
-
- ZeroMemory(ToBuf, sizeof(ToBuf));
-
- ZeroMemory(FromBuf, sizeof(FromBuf));
-
- lstrcpy(FromBuf, strDeleteFile);
-
-
-
- Op.hwnd = NULL;
-
- Op.pFrom = FromBuf;
-
- Op.pTo = ToBuf;
-
- Op.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
-
- Op.fAnyOperationsAborted = FALSE;
-
- Op.hNameMappings = NULL;
-
- Op.lpszProgressTitle = NULL;
-
- Op.wFunc = FO_DELETE;
-
-
-
- SHFileOperation(&Op);
- ===================
-
- 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