xp操作系统
用这个api写了个函数
在普通文件夹下都能替换成功
在system32下,就替换不了
怎么回事?

解决方案 »

  1.   

    因为开启有Windows文件保护系统。你替换文件后,系统会自动C:\Windows\system32\dllcache里面复制文件出来替换掉你的文件,也即还原系统文件。
    具体设置可以去组策略:
    单击“开始→运行”,输入“gpedit.msc”,然后依次展开“计算机配置→管理模板→系统→Windows文件保护
      

  2.   

    是不是参数传得有问题啊???我是这样写的
    bool result = ReplaceFile("e:\\test.txt", "d:\\test.txt", NULL, NULL, NULL, NULL);
    如果两个文件都在e盘下就能成功
    现在一个在d盘,一个在e盘返回结果就是false...
      

  3.   

    GetLastError看返回的是497 498 还是499
      

  4.   

    dwReplaceFlags设个REPLACEFILE_IGNORE_MERGE_ERRORS
      

  5.   

    DWORD result = Getlasterror
    cout<<result
    返回的是2...
      

  6.   

    加入REPLACEFILE_IGNORE_MERGE_ERRORS,返回1176
      

  7.   

    bool result = ReplaceFile("e:\\test.txt", "d:\\test.txt", NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL); 
      

  8.   

    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.
      

  9.   

    你如果需要跨卷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;
       
       } __finally {     if (hReplaced != INVALID_HANDLE_VALUE)
             CloseHandle(hReplaced);
       }   return fSuccess;
    }
    微软已经认为ReplaceFile 是一个鸡肋的API。