Windows API移动文件

function TScanFileThread.MoveFile(FileName: string): Boolean;
var
  F: TShFileOpStruct;
begin
  with F do begin
    Wnd := Handle;
    wFunc := FO_MOVE;
    pFrom := PChar(Directory + FileName + #0#0);
    pTo := PChar(SaveDirectory + FileName + #0#0);
    fFlags := FOF_RENAMEONCOLLISION or FOF_NOCONFIRMMKDIR;
  end;
  if ShFileOperation(F) = 0 then begin
    Result := True;
  end
  else begin
    Result := False;
  end;
end;

一开始pFrom和pTo没有以“#0#0”结尾,结果发现在有的机器上能移动成功,有的机器上SHFileOperation返回1026无法转移,查了一下资料

ShFileOperation中的pFrom和pTo中可以包含多个文件名,文件名之间用 ‘\0′ 分割,最后以两个\0结束。

标签:, ,

相关日志

第一个C++程序

开始学习C++,用惯了java以后觉得这东西真是麻烦。。。
用unicode还挺麻烦的,了解了一下Windows API,学海无涯啊。

  1. #include <Windows.h>
  2. #include <string.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void ListFile(void);
  8.  
  9. int wmain(int argc, TCHAR* argv[]) {
  10.  
  11.     locale loc ("Chinese-simplified");
  12.     wcout.imbue(loc);
  13.  
  14.     if (argc > 1) {
  15.         SetCurrentDirectory(argv[1]);
  16.     }
  17.  
  18.  
  19.     ListFile();
  20.  
  21.     return 0;
  22. }
  23.  
  24. void ListFile(void) {
  25.     WIN32_FIND_DATA fd;
  26.     ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  27.  
  28.     TCHAR currDir[255];
  29.     ZeroMemory(currDir, 255);
  30.    
  31.     GetCurrentDirectory(255, currDir);
  32.  
  33.     HANDLE hFile = FindFirstFile(L"*.*", &fd);
  34.  
  35.     if (hFile != INVALID_HANDLE_VALUE) {
  36.         do {
  37.             if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  38.                     // This is a directory entry. Ignore the “.” and “..” entries.
  39.                     if (! (fd.cFileName[0] == '.' && (fd.cFileName[1] == 0 || fd.cFileName[1] == '.'))) {
  40.                         SetCurrentDirectory(fd.cFileName);
  41.                         wcout << "Directory changed to:" << currDir << "\\" << fd.cFileName << endl;
  42.                         ListFile();
  43.                         SetCurrentDirectory(currDir);
  44.                         wcout << "Directory changed to:" << currDir << endl;
  45.                     }
  46.             } else {
  47.                 wcout << fd.cFileName << endl;
  48.             }
  49.         } while (FindNextFile(hFile, &fd));
  50.     }
  51.     FindClose(hFile);
  52. }
标签:, , , , ,

相关日志

Documents

标签:, , , , ,

相关日志

京ICP备05059555号

收藏 & 分享

Powered by 17fav.com