Windows API移动文件
2007-07-06 08:32 | Filed Under Delphi |
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;
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无法转移,查了一下资料
标签:api, 文件, 移动ShFileOperation中的pFrom和pTo中可以包含多个文件名,文件名之间用 ‘\0′ 分割,最后以两个\0结束。
相关日志
第一个C++程序
2007-04-12 15:21 | Filed Under C/CPP |
开始学习C++,用惯了java以后觉得这东西真是麻烦。。。
用unicode还挺麻烦的,了解了一下Windows API,学海无涯啊。
- #include <Windows.h>
- #include <string.h>
- #include <iostream>
- 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);
- }
相关日志
Documents
2006-09-09 14:25 | Filed Under Life |
- 正则表达式语法
- Html/Javascript/CSS Online Documents
- jQuery Docs - 1.1.2 API
- Struts Taglib Reference
- Visual jQuery 1.1(中文版)
- W3Schools Online Web Tutorials 中文版
相关日志


