编写了一个应用程序,功能是将应用程序所在的当前目录中的“source”文件夹拷贝到指定的目录下,具体的函数调用关系如下:
{
char sourcePath[MAX_PATH];
CString destPath;
pDialog3->m_FileListCtl.GetWindowText(destPath);
//pDialog3->m_FileListCtl是一个无模式对话框的组合框控件
GetCurrentDirectory(MAX_PATH,sourcePath); //获得当前目录 strcat(sourcePath,"\\source"); //指到source文件夹
CString sourPath;
sourPath.Format(_T("%s"),sourcePath); //字符格式转换 ::CopyDirectory(m_hWnd,sourPath,destPath); //拷贝文件夹操作函数
}
此时,我在这儿设置断点后,发现sourPath的路径是:“f:\source”,与我的要求相吻合,但是拷贝时提示“拷贝文件错误”,而我将之改成
::CopyDirectory(m_hWnd,“f:\\source”,destPath)时,文件就能够拷贝了,到底是怎么回事呢? CopyDirectory函数如下:
int CopyDirectory(HWND Handle,LPCTSTR PathOut,LPCTSTR PathIn)
{
SHFILEOPSTRUCT op;
ZeroMemory(&op, sizeof(op));
op.hwnd = Handle; //HWND_DESKTOP;
op.wFunc = FO_COPY;
op.pFrom = PathOut;
op.pTo = PathIn;
op.fFlags = FOF_SIMPLEPROGRESS|FOF_FILESONLY
|FOF_RENAMEONCOLLISION|FOF_WANTMAPPINGHANDLE; int Re = SHFileOperation(&op);
return Re;
}
{
char sourcePath[MAX_PATH];
CString destPath;
pDialog3->m_FileListCtl.GetWindowText(destPath);
//pDialog3->m_FileListCtl是一个无模式对话框的组合框控件
GetCurrentDirectory(MAX_PATH,sourcePath); //获得当前目录 strcat(sourcePath,"\\source"); //指到source文件夹
CString sourPath;
sourPath.Format(_T("%s"),sourcePath); //字符格式转换 ::CopyDirectory(m_hWnd,sourPath,destPath); //拷贝文件夹操作函数
}
此时,我在这儿设置断点后,发现sourPath的路径是:“f:\source”,与我的要求相吻合,但是拷贝时提示“拷贝文件错误”,而我将之改成
::CopyDirectory(m_hWnd,“f:\\source”,destPath)时,文件就能够拷贝了,到底是怎么回事呢? CopyDirectory函数如下:
int CopyDirectory(HWND Handle,LPCTSTR PathOut,LPCTSTR PathIn)
{
SHFILEOPSTRUCT op;
ZeroMemory(&op, sizeof(op));
op.hwnd = Handle; //HWND_DESKTOP;
op.wFunc = FO_COPY;
op.pFrom = PathOut;
op.pTo = PathIn;
op.fFlags = FOF_SIMPLEPROGRESS|FOF_FILESONLY
|FOF_RENAMEONCOLLISION|FOF_WANTMAPPINGHANDLE; int Re = SHFileOperation(&op);
return Re;
}
这里的f:\\source表示的就是f:\source。
“\\”才是“\”。
sourcePath的长度大于实际得到路径的字符所占的长度,所以后面几位是乱码。
你设置断点后,sourPath的路径是:“f:\source”呢,还是“f:\source¥¥¥¥¥¥"呢?
不知道说错了没,请各位指教:)
显示的就是:“f:\source”,
请各位再给点意见!呵呵
#define __T(x) x
还是不行啊,我不知道你怎么改的?你说把sourPath定义成LPCSTR是可以,
是如我下面改的吗?GetCurrentDirectory(MAX_PATH,sourcePath); //获得当前目录strcat(sourcePath,"\\debug\\c\0"); //指到source文件夹
LPCTSTR sourPath = (LPCTSTR )sourcePath; //字符格式转换
::CopyDirectory(m_hWnd,sourPath,destPath); //拷贝文件夹操作函数
}
不好意思,上面有个地方不对!
还是不行啊,我不知道你怎么改的?你说把sourPath定义成LPCSTR是可以,
是如我下面改的吗?GetCurrentDirectory(MAX_PATH,sourcePath); //获得当前目录strcat(sourcePath,"\\source"); //指到source文件夹
LPCTSTR sourPath = (LPCTSTR )sourcePath; //字符格式转换
::CopyDirectory(m_hWnd,sourPath,destPath); //拷贝文件夹操作函数
可以肯定,你的sourPath路径是没问题的。你可以在调用函数时写成:
::CopyDirectory(m_hWnd,destPath,sourPath); //将源和目的路径反过来写。
来验证。
但写成你所要的情况时却确实有问题,我调试了一个多小时,失败了,哎,换一种方法看看。