Movefilex function

In this article i will tell you about movefilex function and it’s uses

So let’s start with what is movefileex function ?

Well it justs Moves an existing file or a directory, including its children.

But you might think hey what the hell ? when we have .net framework why do we need this api ?

well the answer is sometimes you might need to move a file which is different from normal files like files which are in use . well in this case you can use movefileex function with dwFlags value 4 which will replace the file during machine reboot. This is very useful there are other options also that you can specify to dwFlags parameter.

Here is the function provided by windows :

BOOL WINAPI MoveFileEx(
__in LPCTSTR lpExistingFileName,
__in_opt LPCTSTR lpNewFileName,
__in DWORD dwFlags
);

lpExistingFileName [in]
The current name of the file or directory on the local computer.

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT, the file cannot exist on a remote share, because delayed operations are performed before the network is available.

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

Windows 2000: If you prepend the file name with “\\?\”, you cannot also specify the MOVEFILE_DELAY_UNTIL_REBOOT flag for dwFlags.
lpNewFileName [in, optional]
The new name of the file or directory on the local computer.

When moving a file, the destination can be on a different file system or volume. If the destination is on another drive, you must set the MOVEFILE_COPY_ALLOWED flag in dwFlags.

When moving a directory, the destination must be on the same drive.

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.

dwFlags [in]
This parameter can be one or more of the following values.

Value Meaning
MOVEFILE_COPY_ALLOWED
2 (0x2) If the file is to be moved to a different volume, the function simulates the move by using the CopyFile and DeleteFile functions.

This value cannot be used with MOVEFILE_DELAY_UNTIL_REBOOT.

MOVEFILE_CREATE_HARDLINK
16 (0x10) Reserved for future use.

MOVEFILE_DELAY_UNTIL_REBOOT
4 (0x4) The system does not move the file until the operating system is restarted. The system moves the file immediately after AUTOCHK is executed, but before creating any paging files. Consequently, this parameter enables the function to delete paging files from previous startups.

This value can be used only if the process is in the context of a user who belongs to the administrators group or the LocalSystem account.

This value cannot be used with MOVEFILE_COPY_ALLOWED.

Windows Server 2003 and Windows XP: For information about special situations where this functionality can fail, and a suggested workaround solution, see Files are not exchanged when Windows Server 2003 restarts if you use the MoveFileEx function to schedule a replacement for some files in the Help and Support Knowledge Base.
Windows 2000: If you specify the MOVEFILE_DELAY_UNTIL_REBOOT flag for dwFlags, you cannot also prepend the file name that is specified by lpExistingFileName with “\\?”.
MOVEFILE_FAIL_IF_NOT_TRACKABLE
32 (0x20) The function fails if the source file is a link source, but the file cannot be tracked after the move. This situation can occur if the destination is a volume formatted with the FAT file system.

MOVEFILE_REPLACE_EXISTING
1 (0x1) If a file named lpNewFileName exists, the function replaces its contents with the contents of the lpExistingFileName file, provided that security requirements regarding access control lists (ACLs) are met. For more information, see the Remarks section of this topic.

This value cannot be used if lpNewFileName or lpExistingFileName names a directory.

MOVEFILE_WRITE_THROUGH
8 (0x8) The function does not return until the file is actually moved on the disk.

Setting this value guarantees that a move performed as a copy and delete operation is flushed to disk before the function returns. The flush occurs at the end of the copy operation.

This value has no effect if MOVEFILE_DELAY_UNTIL_REBOOT is set

You can see more at http://msdn.microsoft.com/en-us/library/aa365240(v=VS.85).aspx

for vb.net function see this
http://www.pinvoke.net/default.aspx/kernel32.movefileex

Remember you can use this function with other languages too but their sytax might be different .

If you have any suggestions and comments use idealprogrammer forms