CopyFileEx函数路径问题
我想使用CopyFileEx函数从一个文件夹路径把里面的图片复制到我指定的另一个文件夹路径里,现在是图片复制成功了,但只能复制在本路径,而不能跨路径复制,求解.
[code=C/C++][/code]
void Setpersonality::OnOK() //打开一个对话框,选取一张图片
{
// TODO: Add extra validation here
GetDlgItem(IDC_Name)->GetWindowText(Name2);
if(Name2=="")
{
MessageBoxDlg d("请输入有效昵称!","知道了","知道了");
d.DoModal();
return ;
}
Head_choose = strPictureName;//得到选取的图片路径
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
CString szNewPath = "Debug";//当前程序文件夹下的一个子文夹
CreateDirectory(szNewPath,NULL);//Creates a new directory
szNewPath = szNewPath + "\\head";//目的文件夹(Debug\\head)
CreateDirectory(szNewPath, NULL);
BOOL fFinished = FALSE;
// Start searching for .bmp files in the current directory.
hSearch = FindFirstFile("*.bmp", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
MessageBox("No .bmp files found.");
return;
}
// Copy each .bmp file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{ //以下函数第二个参数如果为"XX.bmp"则成功,但改为我指定的路径则失败.....求解。。。。。
if (CopyFileEx(strPictureName,szNewPath,(LPPROGRESS_ROUTINE)CopyProgress,NULL,FALSE,COPY_FILE_FAIL_IF_EXISTS))
{
dwAttrs = GetFileAttributes(FileData.cFileName);//得到文件属性
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
int a =GetLastError();
CString s;
s.Format("error = %d",a);//错误值将设为5(拒绝访问)。
MessageBox(s);
MessageBox("Couldn't copy file.");
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
::MessageBox(NULL, "No more .bmp files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
MessageBox("Couldn't find next file.");
return;
}
}
}
// Close the search handle.
FindClose(hSearch);
CDialog::OnOK();
}
[解决办法]
BOOL CopyFileEx(
LPCTSTR lpExistingFileName, // name of existing file
LPCTSTR lpNewFileName, // name of new file
LPPROGRESS_ROUTINE lpProgressRoutine, // callback function
LPVOID lpData, // callback parameter
LPBOOL pbCancel, // cancel status
DWORD dwCopyFlags // copy options
);
lpExistingFileName和lpNewFileName都用绝对路径.
[解决办法]
写固定的绝对路径,使一下