Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

MoveFile (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool MoveFileWithProgress(string lpExistingFileName,
   string lpNewFileName, CopyProgressRoutine lpProgressRoutine,
   IntPtr lpData, MoveFileFlags dwFlags);

User-Defined Types:

CopyProgressRoutine, MoveFileFlags

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool MoveFileWithProgress(string lpExistingFileName, string lpNewFileName,
       CopyProgressRoutine lpProgressRoutine, IntPtr lpData, MoveFileFlags dwCopyFlags);

    delegate CopyProgressResult CopyProgressRoutine(
    long TotalFileSize,
    long TotalBytesTransferred,
    long StreamSize,
    long StreamBytesTransferred,
    uint dwStreamNumber,
    CopyProgressCallbackReason dwCallbackReason,
    IntPtr hSourceFile,
    IntPtr hDestinationFile,
    IntPtr lpData);

    enum CopyProgressResult : uint
    {
    PROGRESS_CONTINUE = 0,
    PROGRESS_CANCEL = 1,
    PROGRESS_STOP = 2,
    PROGRESS_QUIET = 3
    }

    enum CopyProgressCallbackReason : uint
    {
    CALLBACK_CHUNK_FINISHED = 0x00000000,
    CALLBACK_STREAM_SWITCH = 0x00000001
    }

    [Flags]
    enum MoveFileFlags : uint
    {
    MOVE_FILE_REPLACE_EXISTSING = 0x00000001,
    MOVE_FILE_COPY_ALLOWED = 0x00000002,
    MOVE_FILE_DELAY_UNTIL_REBOOT = 0x00000004,
    MOVE_FILE_WRITE_THROUGH = 0x00000008,
    MOVE_FILE_CREATE_HARDLINK = 0x00000010,
    MOVE_FILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
    }

    private void XCopy(string oldFile, string newFile)
    {
    CopyFileEx(oldFile, newFile, new CopyProgressRoutine(this.CopyProgressHandler), IntPtr.Zero, MoveFileFlags .MOVE_FILE_REPLACE_EXISTSING|MoveFileFlags.MOVE_FILE_WRITE_THROUGH|MoveFileFlags.MOVE_FILE_COPY_ALLOWED);
    }

    private CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long StreamByteTrans, uint dwStreamNumber,CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
    {
    return CopyProgressResult.PROGRESS_CONTINUE;
    }

Alternative Managed API:

File.Move moves a file without a callback or options.

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions