CopyFile函數詳解

CopyFile函數,文件拷贝函数.其基本結構如下:

copyfile(

lpcstr lpexistingfilename, // 源文件路径

lpcstr lpnewfilename, //新文件路径

bool bfailifexists //为true的话, 如果新文件已存在, 则返回false;如果為false的話,如果新文件已經存在,會將原來的

覆蓋.

);

函數成功返回true,失敗返回false;

举例:

CopyFile(pChar('sql.txt'),pChar(ExtractFilePath(Application.ExeName) + '2.txt'),true);

后來在試驗的時候發現,源文件前可以不加pChar函數轉換,但新文件就不可以,會出現類型轉化錯誤.而且當源文件名前不加路徑時,默認是在應

用程序的本目錄下.

因為函數返回的是boolean型,所以也可以這樣寫:

if CopyFile('D:yun_yuesql.txt',pChar(ExtractFilePath(Application.ExeName) + '2.txt'),true) then

//而且,請確保你的文件路徑正確,否則函數返回失敗.

ShowMessage('Copy File Completed!')

else

ShowMessage('Copy File Failed!');

CopyFile函數還可以用與在網絡鄰居中的文件復制,使用上面的例子,可以將格式改為如下:

if CopyFile('\sh-sfisYun_Yueyun_yueCopytext.txt',pChar(ExtractFilePath(Application.ExeName) + '2.txt'),false) then

//此處函數的第三個參數設置成false,會將原來我的應用程序本目錄下存在的'2.txt'文件覆蓋掉

ShowMessage('Copy File Completed!')

else

ShowMessage('Copy File Failed!'); 

 
 
http://blog.csdn.net/zisongjia/article/details/60125901
原文地址:https://www.cnblogs.com/findumars/p/6528620.html