Từ 1 tới 4 trên tổng số 4 kết quả

Đề tài: Hàm CopyFileEx khác hàm CopyFile ở chỗ nào

  1. #1
    Ngày gia nhập
    09 2008
    Bài viết
    328

    Angry Hàm CopyFileEx khác hàm CopyFile ở chỗ nào

    Mình không rõ sự khác biệt giữa CopyFile và CopyFileEx

    CopyFileEx Function

    Copies an existing file to a new file, notifying the application of its progress through a callback function.

    To perform this operation as a transacted operation, use the CopyFileTransacted function.


    Syntax
    Visual C++ Code:
    1. BOOL WINAPI CopyFileEx(
    2.   __in      LPCTSTR lpExistingFileName,
    3.   __in      LPCTSTR lpNewFileName,
    4.   __in_opt  LPPROGRESS_ROUTINE lpProgressRoutine,
    5.   __in_opt  LPVOID lpData,
    6.   __in_opt  LPBOOL pbCancel,
    7.   __in      DWORD dwCopyFlags
    8. );

    Parameters
    lpExistingFileName
    The name of an existing file.

    In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

    If lpExistingFileName does not exist, the CopyFileEx function fails, and the GetLastError function returns ERROR_FILE_NOT_FOUND.

    lpNewFileName
    The name of the new file.

    In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

    lpProgressRoutine
    The address of a callback function of type LPPROGRESS_ROUTINE that is called each time another portion of the file has been copied. This parameter can be NULL. For more information on the progress callback function, see the CopyProgressRoutine function.

    lpData
    The argument to be passed to the callback function. This parameter can be NULL.

    pbCancel
    If this flag is set to TRUE during the copy operation, the operation is canceled. Otherwise, the copy operation will continue to completion.

    dwCopyFlags
    Flags that specify how the file is to be copied. This parameter can be a combination of the following values.

    Value Meaning
    COPY_FILE_ALLOW_DECRYPTED_DESTINATION
    0x00000008
    An attempt to copy an encrypted file will succeed even if the destination copy cannot be encrypted.

    Windows 2000: This value is not supported.
    COPY_FILE_COPY_SYMLINK
    0x00000800
    If the source file is a symbolic link, the destination file is also a symbolic link pointing to the same file that the source symbolic link is pointing to.

    Windows Server 2003 and Windows XP/2000: This value is not supported.
    COPY_FILE_FAIL_IF_EXISTS
    0x00000001
    The copy operation fails immediately if the target file already exists.

    COPY_FILE_OPEN_SOURCE_FOR_WRITE
    0x00000004
    The file is copied and the original file is opened for write access.

    COPY_FILE_RESTARTABLE
    0x00000002
    Progress of the copy is tracked in the target file in case the copy fails. The failed copy can be restarted at a later time by specifying the same values for lpExistingFileName and lpNewFileName as those used in the call that failed.


    Return Value
    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.

    If lpProgressRoutine returns PROGRESS_CANCEL due to the user canceling the operation, CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED. In this case, the partially copied destination file is deleted.

    If lpProgressRoutine returns PROGRESS_STOP due to the user stopping the operation, CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED. In this case, the partially copied destination file is left intact.

    Remarks
    This function preserves extended attributes, OLE structured storage, NTFS file system alternate data streams, and file attributes. Security attributes for the existing file are not copied to the new file. To copy security attributes, use the SHFileOperation function.

    This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY attribute set.

    When encrypted files are copied using CopyFileEx, the function attempts to encrypt the destination file with the keys used in the encryption of the source file. If this cannot be done, this function attempts to encrypt the destination file with default keys. If both of these methods cannot be done, CopyFileEx fails with an ERROR_ENCRYPTION_FAILED error code. If you want CopyFileEx to complete the copy operation even if the destination file cannot be encrypted, include the COPY_FILE_ALLOW_DECRYPTED_DESTINATION as the value of the dwCopyFlags parameter in your call to CopyFileEx.

    Windows 2000: When encrypted files are copied using CopyFileEx, the function attempts to encrypt the destination file with the default keys. No attempt is made to encrypt the destination file with the keys used in the encryption of the source file. If it cannot be encrypted, CopyFileEx completes the copy operation without encrypting the destination file.



    If COPY_FILE_COPY_SYMLINK is specified, the following rules apply:


    If the source file is a symbolic link, the symbolic link is copied, not the target file.
    If the source file is not a symbolic link, there is no change in behavior.
    If the destination file is an existing symbolic link, the symbolic link is overwritten, not the target file.
    If COPY_FILE_FAIL_IF_EXISTS is also specified, and the destination file is an existing symbolic link, the operation fails in all cases.

    If COPY_FILE_COPY_SYMLINK is not specified, the following rules apply:


    If COPY_FILE_FAIL_IF_EXISTS is also specified, and the destination file is an existing symbolic link, the operation fails only if the target of the symbolic link exists.
    If COPY_FILE_FAIL_IF_EXISTS is not specified, there is no change in behavior.

    If you are writing an application that is optimizing file copy operations across a LAN, consider using the TransmitFile function from Windows Sockets (Winsock). TransmitFile supports high-performance network transfers and provides a simple interface to send the contents of a file to a remote computer. To use TransmitFile, you must write a Winsock client application that sends the file from the source computer as well as a Winsock server application that uses other Winsock functions to receive the file on the remote computer.

    To compile an application that uses this function, define the _WIN32_WINNT macro as 0x0400 or later. For more information, see Using the SDK Headers.

    Requirements
    Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.
    Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
    Header Declared in WinBase.h; include Windows.h.

    Library Use Kernel32.lib.

    DLL Requires Kernel32.dll.

    Unicode Implemented as CopyFileExW (Unicode) and CopyFileExA (ANSI).


    See Also
    CopyFile
    CopyFileTransacted
    CopyProgressRoutine
    CreateFile
    File Management Functions
    MoveFile
    MoveFileWithProgress
    Symbolic Links
    TransmitFile



    Send comments about this topic to Microsoft

    Build date: 4/22/2008
    CopyFile Function

    Copies an existing file to a new file.

    The CopyFileEx function provides two additional capabilities. CopyFileEx can call a specified callback function each time a portion of the copy operation is completed, and CopyFileEx can be canceled during the copy operation.

    To perform this operation as a transacted operation, use the CopyFileTransacted function.


    Syntax
    Visual C++ Code:
    1. BOOL WINAPI CopyFile(
    2.   __in  LPCTSTR lpExistingFileName,
    3.   __in  LPCTSTR lpNewFileName,
    4.   __in  BOOL bFailIfExists
    5. );

    Parameters
    lpExistingFileName
    The name of an existing file.

    In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

    If lpExistingFileName does not exist, CopyFile fails, and GetLastError returns ERROR_FILE_NOT_FOUND.

    lpNewFileName
    The name of the new file.

    In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

    bFailIfExists
    If this parameter is TRUE and the new file specified by lpNewFileName 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 Value
    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.

    Remarks
    Security attributes for the existing file are not copied to the new file. To copy security attributes, use the SHFileOperation function.

    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. For more information, see Retrieving and Changing File Attributes.

    This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY attribute set.

    When CopyFile is used to copy an encrypted file, it attempts to encrypt the destination file with the keys used in the encryption of the source file. If this cannot be done, this function attempts to encrypt the destination file with default keys, as in Windows 2000. If neither of these methods can be done, CopyFile fails with an ERROR_ENCRYPTION_FAILED error code.

    Windows 2000: When CopyFile is used to copy an encrypted file, the function attempts to encrypt the destination file with the default keys. No attempt is made to encrypt the destination file with the keys used in the encryption of the source file. If it cannot be encrypted, CopyFile completes the copy operation without encrypting the destination file.

    Symbolic link behavior—If the source file is a symbolic link, the actual file copied is the target of the symbolic link.

    If the destination file already exists and is a symbolic link, the symbolic link is overwritten by the source file.

    Example Code
    For an example, see Retrieving and Changing File Attributes.

    Requirements
    Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.
    Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
    Header Declared in WinBase.h; include Windows.h.

    Library Use Kernel32.lib.

    DLL Requires Kernel32.dll.

    Unicode Implemented as CopyFileW (Unicode) and CopyFileA (ANSI).


    See Also
    CopyFileEx
    CopyFileTransacted
    CreateFile
    File Management Functions
    MoveFile
    Symbolic Links



    Send comments about this topic to Microsoft

    Build date: 4/22/2008
    Tham gia diễn đàn MACOS http://www.apple9.com/forum

  2. #2
    Ngày gia nhập
    07 2008
    Nơi ở
    /media/Anime
    Bài viết
    2,288

    Điểm khác nhau lớn nhất giữa 2 hàm này đó là hàm CopyFileEx có cho đăng ký 1 hàm callback để theo dõi quá trình copy. Nếu copy file nhỏ thì chưa mất tới 1 giây. Nhưng nếu copy file quá lớn, thời gian copy sẽ lâu. Nếu dùng hàm CopyFile nó sẽ bắt ta ngồi chờ cho đến khi xong, nhưng hàm CopyFileEx sẽ gọi hàm callback và báo cho ta biết là nó đã chép tới đâu rồi. Hàm này thường được ứng dụng cho việc copy file đồng thời hiện thanh progress bar xem đã copy tới đâu.
    Càng yêu mèo thì mèo càng mập. Mèo càng mập ta lại càng yêu.

  3. #3
    Ngày gia nhập
    09 2008
    Bài viết
    1

    Trích dẫn Nguyên bản được gửi bởi meoconlongvang Xem bài viết
    Điểm khác nhau lớn nhất giữa 2 hàm này đó là hàm CopyFileEx có cho đăng ký 1 hàm callback để theo dõi quá trình copy. Nếu copy file nhỏ thì chưa mất tới 1 giây. Nhưng nếu copy file quá lớn, thời gian copy sẽ lâu. Nếu dùng hàm CopyFile nó sẽ bắt ta ngồi chờ cho đến khi xong, nhưng hàm CopyFileEx sẽ gọi hàm callback và báo cho ta biết là nó đã chép tới đâu rồi. Hàm này thường được ứng dụng cho việc copy file đồng thời hiện thanh progress bar xem đã copy tới đâu.
    Anh cho em hỏi có cách nào quản lý quá trình Copy không? Em muốn biết được quá trình Copy đã kết thúc chưa, và nếu kết thúc rồi thì mới tiếp tục làm các công việc khác. Liệu có cách nào k?

  4. #4
    Ngày gia nhập
    07 2008
    Nơi ở
    /media/Anime
    Bài viết
    2,288

    Trích dẫn Nguyên bản được gửi bởi bautopteo Xem bài viết
    Anh cho em hỏi có cách nào quản lý quá trình Copy không? Em muốn biết được quá trình Copy đã kết thúc chưa, và nếu kết thúc rồi thì mới tiếp tục làm các công việc khác. Liệu có cách nào k?
    Bạn trích dẫn nội dung trước đó của mình mà lại chẳng buồn đọc coi trong đó viết gì sao ? Cả 2 hàm này đều phải chờ khi copy xong rồi mới chạy tiếp được. Hàm Ex có thông báo trạng thái qua callback lúc đang copy. Như vậy ta có thể kiểm soát hoàn toàn quá trình copy.
    Càng yêu mèo thì mèo càng mập. Mèo càng mập ta lại càng yêu.

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn