在线等待,请大哥们帮我看看这个代码出错在哪里?(100分)

  • 主题发起人 ch_yahuu
  • 开始时间
C

ch_yahuu

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>if fileexists('c:/windows.log')=true then application.Terminate<br>else<br>begin<br>filecreate('c:/windows.log');<br>end;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>deletefile('c:/windows.log');<br>end;<br>end.<br><br><br>为什么程序关闭后不能deletefile('c:/windows.log');???????????????<br>要怎么用呢?<br>谢谢
 
formdestroy 试一下<br>if fileexists('c:/windows.log')=true then<br>&nbsp;deletefile('c:/windows.log');
 
同意楼上
 
应该是在程序中其它地方有打开windows.log,用完后没用释放Free。
 
不行啊。当次退出时还是不能deltefile('c:/windows.log');只有再次运行后才能<br>deletefile('c:/windows.log');<br><br>
 
其它地方也没有打开windows.log。我只是把它做为一个条件在测试软件。
 
将filecreate('c:/windows.log')改为<br>fileclose(filecreate('c:/windows.log'))即可
 
你创建该文件的时候,已经占用了该文件的文件句柄,所以应当释放<br>你可以看一下filecreate的返回值是什么,这样你就明白是为什么不能删除了
 
前两天在看有关文件操作的东西,曾遇到类似问题,也是百思不得其解,<br>后来看了帮助才想到这个问题,也算瞎猫碰到死耗子了,呵呵
 
还有一个问题,就是这个文件句柄究竟是被谁占用了,我到现在也没搞清楚,哪位大虾能把<br>相关的内容解释清楚一下,让在下知已然也知其所以然
 
顶部