为什么文件不能被删除?(50分)

  • 主题发起人 主题发起人 zoulin
  • 开始时间 开始时间
Z

zoulin

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:
本人有一段程序是用来删除ini文件的,可是不知为什么文件总是不能被正确删除。程序如下:
setdate1:=tsetdate1.Create(Application);
setdate1.Caption:=setdate1.Caption+_filtratechart((sender as TMenuItem).caption);
setdate1.ShowModal;
setdate1.Free;
if _dialogok then
begin
sthint1.Show;
sthint1.Update;
toassure1:=ttoassure1.Create(Application);
toassure1.Caption:=_filtratechart((sender as TMenuItem).caption);
sthint1.close;
toassure1.ShowModal;
toassure1.Free;
end;
_delfile(_inilistview)

unit setdate;
interface
uses
wpublic,SysUtils, Windows, Forms, StdCtrls,Dialogs, Mask, ToolEdit,
Controls, Buttons, Classes, RXCtrls;
type
Tsetdate1 = class(TForm)
RxLabel1: TRxLabel;
RxLabel2: TRxLabel;
btnok: TBitBtn;
btnexit: TBitBtn;
bgndate: TDateEdit;
enddate: TDateEdit;
procedure FormCreate(Sender: TObject);
procedure btnokClick(Sender: TObject);
procedure FormKeyPress(Sender: TObject;
var Key: Char);
end;

var
setdate1: Tsetdate1;
implementation
{$R *.DFM}
procedure Tsetdate1.FormCreate(Sender: TObject);
begin
_inilistview:=_clidir[5]+_Randomstring+'.ini';
_dialogok:=false;
bgndate.date:=date;
enddate.date:=date;
end;

procedure Tsetdate1.btnokClick(Sender: TObject);
begin
if bgndate.date>enddate.date then
begin
messagebeep(MB_ICONQUESTION);
messagedlg('您输入的结束日期必须大于等于开始日期...',mterror,[mbcancel],0);
enddate.SetFocus;
abort;
end;
_writeinidata(_inilistview,'date','bgndate',datetostr(bgndate.date));
_writeinidata(_inilistview,'date','enddate',datetostr(enddate.date));
_dialogok:=true;
close;
end;

procedure Tsetdate1.FormKeyPress(Sender: TObject;
var Key: Char);
begin
case key of
#13:
begin
Key:=Chr(0);
SelectNext(ActiveControl,True,True);
end;
#27: close;
end;
end;

end.


unit toassure;
interface
uses
wpublic,SysUtils,Forms,graphics,IniFiles, Db, DBTables, Grids, DBGrids,
RXDBCtrl, StdCtrls, Buttons, DBCtrls, Controls, ExtCtrls, Classes;
type
Ttoassure1 = class(TForm)
Panel2: TPanel;
DBNavigator1: TDBNavigator;
btnfind: TBitBtn;
BitBtn6: TBitBtn;
btnprint: TBitBtn;
RxDBGrid1: TRxDBGrid;
btnfilter: TSpeedButton;
btnunfilter: TSpeedButton;
Qbonded: TQuery;
Qdraw: TQuery;
Table1: TTable;
procedure FormCreate(Sender: TObject);
procedure BitBtn6Click(Sender: TObject);
procedure btnfilterClick(Sender: TObject);
procedure btnunfilterClick(Sender: TObject);
procedure btnprintClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
toassure1: Ttoassure1;
implementation
uses setdate,setfilter, dmdata, printfield, re_toassure;

{$R *.DFM}
procedure Ttoassure1.FormCreate(Sender: TObject);
var
p_tempdb:string;
p_inifile:TIniFile;
//表结构存放ini文件
p_bgnd,p_endd:tdate;
p_acnum:string;
p_bondedmoney:real;
p_drawmoney:real;
begin
p_tempdb:=_clidir[5]+'assuretotal.db';
p_bgnd:=strtodate(_readinidata(_inilistview,'date','bgndate'));
p_endd:=strtodate(_readinidata(_inilistview,'date','enddate'));
_createtable(p_tempdb);
dmdata1.Tassuretotal.DatabaseName:=_alias;
dmdata1.Tassuretotal.TableName:=p_tempdb;
dmdata1.Tassuretotal.Open;
table1.DatabaseName:=_alias;
table1.TableName:=_serdir[5]+'account.db';
table1.IndexFieldNames:='acnum';
table1.Filtered:=true;
table1.Filter:='ynstop=false';
table1.Open;
Qbonded.UnPrepare;
Qbonded.ParamByName('_bgndate').asdatetime:=p_bgnd;
Qbonded.ParamByName('_enddate').asdatetime:=p_endd;
Qbonded.Prepare;
Qbonded.Open;
Qdraw.UnPrepare;
Qdraw.ParamByName('_bgndate').asdatetime:=p_bgnd;
Qdraw.ParamByName('_enddate').asdatetime:=p_endd;
Qdraw.Prepare;
Qdraw.Open;
table1.First;
while not table1.Eofdo
begin
p_acnum:=table1.fieldbyname('acnum').asstring;
p_bondedmoney:=0;
p_drawmoney:=0;
if _findquery(Qbonded,'acnum',p_acnum,false) then
p_bondedmoney:=Qbonded.FieldByName('trademoney').asfloat;
if _findquery(Qdraw,'acnum',p_acnum,false) then
p_drawmoney:=Qdraw.FieldByName('trademoney').asfloat;
if (p_drawmoney<>0) or (p_bondedmoney<>0) then
begin
dmdata1.Tassuretotal.append;
dmdata1.Tassuretotal.fieldbyname('num_ywy').asstring:=table1.fieldbyname('num_ywy').asstring;
dmdata1.Tassuretotal.fieldbyname('acnum').asstring:=p_acnum;
dmdata1.Tassuretotal.fieldbyname('bondedmoney').asfloat:=p_bondedmoney;
dmdata1.Tassuretotal.fieldbyname('drawmoney').asfloat:=p_drawmoney;
dmdata1.Tassuretotal.fieldbyname('balancemoney').asfloat:=p_bondedmoney+p_drawmoney;
dmdata1.Tassuretotal.post;
end;
table1.Next;
end;
table1.Close;
Qbonded.Close;
Qdraw.Close;
dmdata1.Tassuretotal.First;
btnfind.Enabled:=dmdata1.Tassuretotal.RecordCount>0;
btnprint.Enabled:=dmdata1.Tassuretotal.RecordCount>0;
btnfilter.Enabled:=dmdata1.Tassuretotal.RecordCount>0;
btnunfilter.Enabled:=dmdata1.Tassuretotal.RecordCount>0;
_setdbgridcolumnproper(rxdbgrid1);
dmdata1.query1.Close;
dmdata1.query1.UnPrepare;
dmdata1.query1.SQL.clear;
dmdata1.query1.SQL.add('select acnum from "'+p_tempdb+'" group by acnum order by acnum');
dmdata1.query1.Prepare;
dmdata1.query1.Open;
dmdata1.query1.First;
p_inifile:=TIniFile.Create(_inilistview);
p_inifile.writeString('name','name',caption);
p_inifile.writeString('list','remark','acnum');
while not dmdata1.query1.Eofdo
begin
p_inifile.writeString('list','lv'+inttostr(dmdata1.query1.RecNo),
dmdata1.query1.FieldByName('acnum').asstring);
p_inifile.writeString('data',dmdata1.query1.FieldByName('acnum').asstring,'1');
dmdata1.query1.Next;
end;
dmdata1.query1.Close;
dmdata1.query1.UnPrepare;
dmdata1.query1.SQL.clear;
dmdata1.query1.SQL.add('select num_ywy from "'+p_tempdb+'" group by num_ywy order by num_ywy');
dmdata1.query1.Prepare;
dmdata1.query1.Open;
p_inifile.writeString('listb','remark','num_ywy');
dmdata1.query1.First;
while not dmdata1.query1.Eofdo
begin
p_inifile.writeString('listb','lv'+inttostr(dmdata1.query1.RecNo),
dmdata1.query1.FieldByName('num_ywy').asstring);
p_inifile.writeString('datab',dmdata1.query1.FieldByName('num_ywy').asstring,'1');
dmdata1.query1.Next;
end;
p_inifile.free;
end;

procedure Ttoassure1.BitBtn6Click(Sender: TObject);
begin
close;
end;

procedure Ttoassure1.btnfilterClick(Sender: TObject);
var
p_inifile:TIniFile;
p_acnum,p_num_ywy:string;
begin
setfilter1:=tsetfilter1.Create(Application);
setfilter1.ShowModal;
setfilter1.Free;
if _dialogok then
begin
p_inifile:=TIniFile.Create(_inilistview);
dmdata1.Tassuretotal.DisableControls;
dmdata1.Tassuretotal.Filtered:=false;
dmdata1.Tassuretotal.First;
while not dmdata1.Tassuretotal.Eofdo
begin
p_num_ywy:=dmdata1.Tassuretotal.FieldByName('num_ywy').asstring;
p_acnum:=dmdata1.Tassuretotal.FieldByName('acnum').asstring;
p_acnum:=p_inifile.readstring('data',p_acnum,'');
p_num_ywy:=p_inifile.readstring('datab',p_num_ywy,'');
dmdata1.Tassuretotal.Edit;
if (p_acnum='1') and (p_num_ywy='1')
then
dmdata1.Tassuretotal.FieldByName('myfilter').asstring:='*'
else
dmdata1.Tassuretotal.FieldByName('myfilter').asstring:='';
dmdata1.Tassuretotal.post;
dmdata1.Tassuretotal.Next;
end;
dmdata1.Tassuretotal.Filtered:=true;
dmdata1.Tassuretotal.First;
dmdata1.Tassuretotal.EnableControls;
dmdata1.Tassuretotal.Refresh;
p_inifile.free;
end;
end;

procedure Ttoassure1.btnunfilterClick(Sender: TObject);
begin
dmdata1.Tassuretotal.Filtered:=false;
dmdata1.Tassuretotal.Refresh;
end;

procedure Ttoassure1.btnprintClick(Sender: TObject);
begin
re_toassure1.preview;
end;

procedure Ttoassure1.FormDestroy(Sender: TObject);
begin
dmdata1.Tassuretotal.Close;
dmdata1.Tassuretotal.DeleteTable;
end;

end.


unit wpublic;
interface
uses
SysUtils,Forms,windows,IniFiles,menus,Dialogs,db,dbtables,dbgrids,bde,
Controls;
var
_inilistview:string;
//保存用于ini文件名称
_dialogok:boolean;
//按下自定义对话框的确认键
_alias: String;
//系统数据引擎
procedure _writeinidata(_filename,_key,_data,_value:string);
function _readinidata(_filename,_key,_data:string;_ynvalue:boolean=true):string;
function _delfile(_fn:string):boolean;
//删除文件
implementation

procedure _writeinidata(_filename,_key,_data,_value:string);
var
_inifile:TIniFile;
begin
_inifile:=TIniFile.Create(_filename);
_inifile.writeString(_key,_data,_value);
_inifile.free;
end;

function _delfile(_fn:string):boolean;
//删除文件
var
p_char:pchar;
begin
if FileExists(_fn) then
begin
getmem(p_char,length(trim(_fn))+1);
StrPCopy(p_char,_fn);
result:=DeleteFile(p_char);
end else
result:=false;
end;

end.
 
function _delfile(_fn:string):boolean;
//经测试可删除文件
你可在调用_delfile之前加断点,运行至此时检查 _fn 是否为完整文件名!
及文件的属性,其是否可被删除!
function _delfile(_fn:string):boolean;
// 有Bug;
1.getmem(p_char...) 后未释放内存
2.整个函数可用一句话代替:
DeleteFile(PChar(_fn));
何必费如此大劲!(你一定是玩C++的吧!)
 
var
filename:string;
f:textfile;
begin
filename:=‘ddd.ini’//删除的文件名。
if fileexists(filename) then
begin
assignfile(f,filename);
Reset(F);
CloseFile(F);
Erase(F);
end;
 
不是有DELETEFILE的函数么?在获得文件名之后,要完全FREE这个文件
然后就可以删除了。(好象太简单了吧,还是因为我没耐心看完上面的
那段程序??)
 
oopsware
我曾经看过一篇文章里说过,在使用pchar数据类型时最好先进行内存申请,否则容易引起系统崩溃。
现在我想请教的是如何释放已经申请的内存(getmem(p_char...) 后未释放内存)?
 
function _delfile(_fn:string):boolean;
//删除文件
var
p_char:pchar;
begin
if FileExists(_fn) then
begin
getmem(p_char,length(trim(_fn))+1);
StrPCopy(p_char,_fn);
result:=DeleteFile(p_char);
FreeMem(p_char);
// 注意释放,即可
end else
result:=false;
end;

如使用string,Delphi会自动为你分配内存,如不是自己创建,没必要释放
@(string[1])、PChar(string) 同你的用法等效!

 
oopsware
今天对代码再次测试发现如果注释掉procedure Ttoassure1.FormCreate过程中的关于写ini文件部分的代码,_delfile是可以正确删除文件的。看来问题是出在procedure Ttoassure1.FormCreate过程中,能帮我找下原因吗?
 
procedure Tsetdate1.btnokClick(Sender: TObject);
中好象未关闭ini文件
删除时会出共享冲突!
 
OOPSWARE:
能具体一点吗?
 
删除Ini文件前,先关闭Ini文件(inifile.Free)
 
oopsware

我想请你帮我检查一下程序在什么地方缺少释放inifile.free.
 
原因我已经找到,感谢各位。
 
后退
顶部