To beta,
您的方法似乎有问题,我注释了下面一句
//Result := Result and DeleteFile(filename);
当我执行XDelFile('c:/1.txt')后,发现1.txt中并没有全部写0,文件是空白的,
我想当程序执行ReWrite语句时,就已将文件删除了(仅仅是删除了文件的分区表记录)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function XDelFile(const filename: string): Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
function TForm1.XDelFile(const filename: string): Boolean;
var
f: file of byte;
n: byte;
i, io: integer;
begin
if FileExists(filename) then
begin
AssignFile(f, filename);
{$I-}
ReWrite(f);
io := IOResult;
{$I+}
CloseFile(f);
if io = 0 then
begin
//第一次,全部写 0
AssignFile(f, filename);
ReWrite(f);
n := 0;
for i := 1 to FileSize(f) do
begin
BlockWrite(f, n, 1);
end;
CloseFile(f);
result:=true;
end
else
begin
Result := False;
end;
//最后,删除文件,无论是否已经安全的填充了字符
//Result := Result and DeleteFile(filename);
end
else
begin
Result := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if self.XDelFile(edit1.text) then
showmessage('Success');;
end;
end.