当目标文件存在且只读时如何用CopyFile覆盖该文件?(100分)

  • 主题发起人 主题发起人 macerate
  • 开始时间 开始时间
M

macerate

Unregistered / Unconfirmed
GUEST, unregistred user!
用Windows API CopyFile(PChar(SourceFile), PChar(DstFile);<br>当DstFile存在且只读时,返回错误值。请问此时如何能将DstFile<br>覆盖?
 
用程序改变DstFile的只读属性,我回家查查。<br>
 
找到了,delphi5 help:<br>function FileSetAttr(const FileName: string; Attr: Integer): Integer;<br>这个函数改变文件属性。<br><br>Description<br><br>FileSetAttr sets the file attributes of the file given by FileName to <br>the value given by Attr. The value of Attr is formed by combining <br>the appropriate file attribute constants, as in the following: <br><br>FileSetAttr('MyFile.sys', faReadOnly or faSysFile);<br><br>FileSetAttr returns zero if the function was successful. Otherwise the return value is a Windows error code.
 
&nbsp; &nbsp; &nbsp; &nbsp;var<br>&nbsp; &nbsp; &nbsp; &nbsp; ats1:word;<br>&nbsp; &nbsp; &nbsp; &nbsp; ss3:string//文件名加路径<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ats1:=filegetattr(ss3); //读属性<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filesetattr(ss3,32); &nbsp;//改属性可写<br>&nbsp; &nbsp; &nbsp; &nbsp;..................................<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;干坏事 <br>&nbsp; &nbsp; &nbsp; &nbsp;..................................<br>&nbsp; &nbsp; &nbsp; &nbsp;filesetattr(ss3 ,ats1);//还原属性<br>&nbsp; &nbsp; &nbsp; &nbsp;end;
 
copyfile('c:/ls.txt','F:/ls1.txt',False);
 
copyfile('c:/ls.txt','F:/ls1.txt',False);<br>
 
改文件属性为normal, then del then copyfile;
 
用SHFileOperation函数好像没有这个问题。
 
接受答案了.
 
后退
顶部