bool result = ReplaceFile("e:\\test.txt", "d:\\test.txt", NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL);
1176=0x498ERROR_UNABLE_TO_MOVE_REPLACEMENTUnable to move the replacement file to the file to be replaced. The file to be replaced has retained its original name.MSDN里面说得很清楚了 The backup file, replaced file, and replacement file must all reside on the same volume.
你如果需要跨卷ReplaceFile的话,需要用如下的函数。 BOOL SimpleReplaceFile(LPTSTR szReplaced, LPTSTR szReplacement) { HANDLE hReplaced = INVALID_HANDLE_VALUE; BOOL fSuccess = FALSE; WIN32_FILE_ATTRIBUTE_DATA fad; __try { // Validate parameters. if (szReplaced == NULL || szReplacement == NULL) { SetLastError(ERROR_INVALID_PARAMETER); __leave; } // Retrieve attributes from the file to be replaced. if (!GetFileAttributesEx(szReplaced, GetFileExInfoStandard, &fad)) __leave; // Delete the file that is being replaced. if (!DeleteFile(szReplaced)) __leave; // Rename the replacement file. if (!MoveFile(szReplacement, szReplaced)) __leave; // This is enough to report success. fSuccess = TRUE; // Try to preserve the following attributes from the original file: fad.dwFileAttributes &= FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_TEMPORARY; if (!SetFileAttributes(szReplaced, fad.dwFileAttributes)) __leave; // Try to set the creation time to match the original file. hReplaced = CreateFile(szReplaced, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL); if (hReplaced == NULL) __leave; if (!SetFileTime(hReplaced, &(fad.ftCreationTime), NULL, NULL)) __leave;
具体设置可以去组策略:
单击“开始→运行”,输入“gpedit.msc”,然后依次展开“计算机配置→管理模板→系统→Windows文件保护
bool result = ReplaceFile("e:\\test.txt", "d:\\test.txt", NULL, NULL, NULL, NULL);
如果两个文件都在e盘下就能成功
现在一个在d盘,一个在e盘返回结果就是false...
cout<<result
返回的是2...
The backup file, replaced file, and replacement file must all reside on the same volume.
BOOL SimpleReplaceFile(LPTSTR szReplaced, LPTSTR szReplacement) { HANDLE hReplaced = INVALID_HANDLE_VALUE;
BOOL fSuccess = FALSE; WIN32_FILE_ATTRIBUTE_DATA fad; __try { // Validate parameters.
if (szReplaced == NULL || szReplacement == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
__leave;
} // Retrieve attributes from the file to be replaced.
if (!GetFileAttributesEx(szReplaced, GetFileExInfoStandard, &fad))
__leave; // Delete the file that is being replaced.
if (!DeleteFile(szReplaced))
__leave; // Rename the replacement file.
if (!MoveFile(szReplacement, szReplaced))
__leave; // This is enough to report success.
fSuccess = TRUE; // Try to preserve the following attributes from the original file:
fad.dwFileAttributes &= FILE_ATTRIBUTE_ARCHIVE
| FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL
| FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
| FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_TEMPORARY;
if (!SetFileAttributes(szReplaced, fad.dwFileAttributes))
__leave; // Try to set the creation time to match the original file.
hReplaced = CreateFile(szReplaced, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, 0, NULL);
if (hReplaced == NULL)
__leave; if (!SetFileTime(hReplaced, &(fad.ftCreationTime), NULL, NULL))
__leave;
} __finally { if (hReplaced != INVALID_HANDLE_VALUE)
CloseHandle(hReplaced);
} return fSuccess;
}
微软已经认为ReplaceFile 是一个鸡肋的API。