Using ReplaceFile() to replace a file

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Replace a file

To show: ReplaceFile()

 

// For WinXp Pro target

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

 

// The file does not need to exist...

LPCWSTR lpReplacedFileName = L"C:\\module20.txt";

LPCWSTR lpReplacementFileName = L"C:\\testfile.txt";

LPVOID lpExclude;

LPVOID lpReserved;

 

int main(void)

{

BOOL test;

 

test = ReplaceFile(

lpReplacedFileName,

lpReplacementFileName,

L"C:\\backup.txt",

REPLACEFILE_IGNORE_MERGE_ERRORS,

lpExclude,

lpReserved

);

 

if(!test)

{

printf("The original file = %S\n", lpReplacedFileName);

printf("The replacement file = %S\n", lpReplacementFileName);

printf("The return value = %d\n", test);

}

else

printf("ReplaceFile() is failed!\n");

return 0;

}

 

Output example:

 

The original file = C:\module20.txt

The replacement file = C:\testfile.txt

The return value = 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index