标签归档:api

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,学海无涯啊。
[coolcode lang=”cpp”]
#include
#include
#include

using namespace std;

void ListFile(void);

int wmain(int argc, TCHAR* argv[]) {

locale loc (“Chinese-simplified”);
wcout.imbue(loc);

if (argc > 1) {
SetCurrentDirectory(argv[1]);
}

ListFile();

return 0;
}

void ListFile(void) {
WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));

TCHAR currDir[255];
ZeroMemory(currDir, 255);

GetCurrentDirectory(255, currDir);

HANDLE hFile = FindFirstFile(L”*.*”, &fd);

if (hFile != INVALID_HANDLE_VALUE) {
do {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// This is a directory entry. Ignore the “.” and “..” entries.
if (! (fd.cFileName[0] == ‘.’ && (fd.cFileName[1] == 0 || fd.cFileName[1] == ‘.’))) {
SetCurrentDirectory(fd.cFileName);
wcout << "Directory changed to:" << currDir << "\\" << fd.cFileName << endl; ListFile(); SetCurrentDirectory(currDir); wcout << "Directory changed to:" << currDir << endl; } } else { wcout << fd.cFileName << endl; } } while (FindNextFile(hFile, &fd)); } FindClose(hFile); } [/coolcode]