在VB里,大家都用哪些方法复制文件呢?
哪种方法最好?

解决方案 »

  1.   

    用 SHFileOperation 这个 API 函数
      

  2.   

    SHFileOperation的参数是一个SHFILEOPSSTRUCT结构。这个结构中各成员的含义如下: 
         - hwnd - 显示文件操作对话框的窗口句柄 
         - wFunc - 表示要进行的操作,可以取以下值: 
         - FO_COPY - 拷贝文件。所要拷贝的文件由pFrom成员指定,目的地址有pTo成员指定。 
         - FO_DELETE - 删除pFrom指定的文件。(pTo 被忽略。) 
         - FO_MOVE - 移动文件。所要移动的文件由pFrom成员指定,目的地址有pTo成员指定。 
         - FO_RENAME - 改名pFrom指定的文件。 
         - pFrom - 指定文件名的缓冲区的地址。必须以Chr(0)结尾。如果包括多个文件以Chr(0)分割。 
         - pTo - 指定目的文件名或目录的缓冲区的地址。必须以Chr(0)结尾。如果使用了FOF_MULTIDESTFILES标志,可以包括多个文件名,文件名之间以Chr(0)分割。 
         - fFlags - 标志: 
         - FOF_ALLOWUNDO - 允许恢复 
         - FOF_FILESONLY - 如果使用了*.*,只操作文件。 
         - FOF_MULTIDESTFILES - pTo成员可以为多个目的文件。 
         - FOF_NOCONFIRMATION - 不显示确认对话框。 
         - FOF_NOCONFIRMMKDIR - 不确认是否建立目录。 
         - FOF_NOERRORUI - 如果有错误,不显示用户界面。 
         - FOF_RENAMEONCOLLISION - 如果目的文件已经存在,给要处理的文件一个新名字。 
         - FOF_SILENT - 不显示进度对话框。 
         - FOF_SIMPLEPROGRESS - 显示进度框,但不显示文件名。 
         - fAnyOperationsAborted -如果用户退出,该成员为TRUE,否则为FALSE。 
         - lpszProgressTitle - 进度框的标题,只有选择了FOF_SIMPLEPROGRESS标志才有效。 
         
        下面是一个例子显示如何拷贝文件: 
        -------------------- 
        1. 在Visual Basic中启动一个新的EXE工程,其中包括Form1。 
        2. 添加两个检查框和一个按钮在Form1上。 
        3. 加入以下代码到Form1的代码窗口: 
         Option Explicit 
         
         Private Const FO_COPY = &H2& 'Copies the files specified 
         'in the pFrom member to the 
         'location specified in the 
         'pTo member. 
         
         Private Const FO_DELETE = &H3& 'Deletes the files specified 
         'in pFrom (pTo is ignored.) 
         
         Private Const FO_MOVE = &H1& 'Moves the files specified 
         'in pFrom to the location 
         'specified in pTo. 
         
         Private Const FO_RENAME = &H4& 'Renames the files 
         'specified in pFrom. 
         
         Private Const FOF_ALLOWUNDO = &H40& 'Preserve Undo information. 
         
         Private Const FOF_CONFIRMMOUSE = &H2& 'Not currently implemented. 
         
         Private Const FOF_CREATEPROGRESSDLG = &H0& 'handle to the parent 
         'window for the 
         'progress dialog box. 
         
         Private Const FOF_FILESONLY = &H80& 'Perform the operation 
         'on files only if a 
         'wildcard file name 
         '(*.*) is specified. 
         
         Private Const FOF_MULTIDESTFILES = &H1& 'The pTo member 
         'specifies multiple 
         'destination files (one 
         'for each source file) 
         'rather than one 
         'directory where all 
         'source files are 
         'to be deposited. 
         
         Private Const FOF_NOCONFIRMATION = &H10& 'Respond with Yes to 
         'All for any dialog box 
         'that is displayed. 
         
         Private Const FOF_NOCONFIRMMKDIR = &H200& 'Does not confirm the 
         'creation of a new 
         'directory if the 
         'operation requires one 
         'to be created. 
         
         Private Const FOF_RENAMEONCOLLISION = &H8& 'Give the file being 
         'operated on a new name 
         'in a move, copy, or 
         'rename operation if a 
         'file with the target 
         'name already exists. 
         
         Private Const FOF_SILENT = &H4& 'Does not display a 
         'progress dialog box. 
         
         Private Const FOF_SIMPLEPROGRESS = &H100& 'Displays a progress 
         'dialog box but does 
         'not show the 
         'file names. 
         
         Private Const FOF_WANTMAPPINGHANDLE = &H20& 
         'If FOF_RENAMEONCOLLISION is specified, 
         'the hNameMappings member will be filled 
         'in if any files were renamed. 
         
         ' The SHFILOPSTRUCT is not double-word aligned. If no steps are 
         ' taken, the last 3 variables will not be passed correctly. This 
         ' has no impact unless the progress title needs to be changed. 
         
         Private Type SHFILEOPSTRUCT 
         hwnd As Long 
         wFunc As Long 
         pFrom As String 
         pTo As String 
         fFlags As Integer 
         fAnyOperationsAborted As Long 
         hNameMappings As Long 
         lpszProgressTitle As String 
         End Type 
         
         Private Declare Sub CopyMemory Lib "KERNEL32" _ 
         Alias "RtlMoveMemory" _ 
         (hpvDest As Any, _ 
         hpvSource As Any, _ 
         ByVal cbCopy As Long) 
         
         Private Declare Function SHFileOperation Lib "Shell32.dll" _ 
         Alias "SHFileOperationA" _ 
         (lpFileOp As Any) As Long 
         
         Private Sub Form_Load() 
         Check1.Caption = "Copy All Files in VB Directory" 
         Check2.Caption = "Display Custom Message" 
         Command1.Caption = "Copy Files" 
         End Sub 
         
         Private Sub Command1_Click() 
         Dim result As Long 
         Dim lenFileop As Long 
         Dim foBuf() As Byte 
         Dim fileop As SHFILEOPSTRUCT 
         
         lenFileop = LenB(fileop) ' double word alignment increase 
         ReDim foBuf(1 To lenFileop) ' the size of the structure. 
         
         With fileop 
         .hwnd = Me.hwnd 
         
         .wFunc = FO_COPY 
         
         ' The files to copy separated by Nulls and terminated by two 
         ' nulls 
         If Check1.Value = vbChecked Then 
         .pFrom = Environ("windir") & "\*.exe" 
         .fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY 
         Else 
         .pFrom = Environ("windir") & "\Explorer.exe" _ 
         & vbNullChar _ 
         & Environ("windir") & "\WinHelp.exe" _ 
         & vbNullChar _ 
         & vbNullChar 
         End If 
         
         .pTo = "C:\testfolder\" & vbNullChar & vbNullChar 
         
         If Check2.Value = vbChecked Then 
         .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or _ 
         FOF_NOCONFIRMMKDIR 
         .lpszProgressTitle = "Your custom dialog string " & _ 
         "appears here." & vbNullChar _ 
         & vbNullChar 
         End If 
         End With 
         
         ' Now we need to copy the structure into a byte array 
         Call CopyMemory(foBuf(1), fileop, lenFileop) 
         
         ' Next we move the last 12 bytes by 2 to byte align the data 
         Call CopyMemory(foBuf(19), foBuf(21), 12) 
         result = SHFileOperation(foBuf(1)) 
         
         If result <> 0 Then ' Operation failed 
         MsgBox Err.LastDllError 'Show the error returned from 
         'the API. 
         Else 
         If fileop.fAnyOperationsAborted <> 0 Then 
         MsgBox "Operation Failed" 
         End If 
         End If 
         End Sub 原文地址:http://www.china-askpro.com/msg8/qa86.shtml
      

  3.   

    FileCopy好了有VB内置的函数就用内置,没有的话再考虑API和引用对象
      

  4.   

    CopyFile
    The CopyFile function copies an existing file to a new file. BOOL CopyFile(
      LPCTSTR lpExistingFileName,
                              // pointer to name of an existing file
      LPCTSTR lpNewFileName,  // pointer to filename to copy to
      BOOL bFailIfExists      // flag for operation if file exists
    );
     
    Parameters
    lpExistingFileName 
    Pointer to a null-terminated string that specifies the name of an existing file. 
    lpNewFileName 
    Pointer to a null-terminated string that specifies the name of the new file. 
    bFailIfExists 
    Specifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds. 
    Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res
    Security attributes for the existing file are not copied to the new file. File attributes for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute. Windows CE: If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Requires version 1.0 or later.
      Header: Declared in winbase.h.
      Import Library: Use kernel32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT.See Also
    File I/O Overview, File Functions, CreateFile, MoveFile 
      

  5.   

    呵呵,在VB中,
    1、如果不在同一文件夹中,简单的文件复制,用NAME 语句。
    2、利用文件系统,filecopy也行。
    3、调用API。
      

  6.   

    FileCopy
    它最终会转为对CopyFile这个API的调用Windows系统知道硬件细节,由它来复制是最快的
      

  7.   

    //怎样才能让速度快一点呢?
    只要设置缓冲区(Byte数组)大小合适