我用SHFileOperation给指定的文件改名,程序如下:SHFILEOPSTRUCT shFileOpStruct;
shFileOpStruct.pFrom=strFrom;
shFileOpStruct.pTo=strTo; //
shFileOpStruct.fFlags =FOF_RENAMEONCOLLISION; //
shFileOpStruct.wFunc=FO_RENAME;int nReturn=::SHFileOperation(&shFileOpStruct);注:strFrom/strTo是CString类,
strFrom是要改的文件的全路径,strTo是被改后的全路径名.如:strFrom="c:\\a\\a.txt",strTo="c:\\a\\b.txt")
但执行结果返回nRetrun=31,
请高手指导一下.
shFileOpStruct.pFrom=strFrom;
shFileOpStruct.pTo=strTo; //
shFileOpStruct.fFlags =FOF_RENAMEONCOLLISION; //
shFileOpStruct.wFunc=FO_RENAME;int nReturn=::SHFileOperation(&shFileOpStruct);注:strFrom/strTo是CString类,
strFrom是要改的文件的全路径,strTo是被改后的全路径名.如:strFrom="c:\\a\\a.txt",strTo="c:\\a\\b.txt")
但执行结果返回nRetrun=31,
请高手指导一下.
CFile::Rename()
能实现文件夹改名吗?
rename("d:\\File1.txt","d:\\File2.doc");
Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft® MS-DOS® wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. (注意)Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom.
2、shFileOpStruct.pFrom指向的字符串必须以2个0字符结尾,而不是普通
的以1个0字符结尾的字符串。为什么了?这是因为,shFileOpStruct.pFrom
可以包含多个文件名,每个文件名之间用1个0字符分隔,所以结束时需要2个
0字符。所以你不能直接使用CString类,而需要定义一块内存,确保字符串
以2个0字符结尾。
经过测试对文件和文件夹都可以。