|
|
|
操作系统: win2000
编程工具: delphi5
问题: 如何删除指定的文件夹和文件?
删除目录可以使用RemoveDir或者RmDir。
RemoveDir例子:
// Create a new directory in the current directory
if CreateDir('TestDir')
then ShowMessage('New directory added OK')
else ShowMessage('New directory add failed with error : '+
IntToStr(GetLastError));
// Remove this directory
if RemoveDir('TestDir')
then ShowMessage('TestDir removed OK')
else ShowMessage('TestDir remove failed with error : '+
IntToStr(GetLastError));
RmDir例子:
var
error : Integer;
begin
// Try to create a new subdirectory in the current directory
// Switch off I/O error checking
{$IOChecks off}
MkDir('TempDirectory');
// Did the directory get created OK?
error := IOResult;
if error = 0
then ShowMessage('Directory created OK')
else ShowMessageFmt('Directory creation failed with error %d',[error]);
// Delete the directory to tidy up
RmDir('TempDirectory');
// Did the directory get removed OK?
error := IOResult;
if error = 0
then ShowMessage('Directory removed OK')
else ShowMessageFmt('Directory removal failed with error %d',[error]);
end;
删除单个文件可以使用DeleteFile。例子:
var
fileName : string;
myFile : TextFile;
data : string;
begin
// Try to open a text file for writing to
fileName := 'Test.txt';
AssignFile(myFile, fileName);
ReWrite(myFile);
// Write to the file
Write(myFile, 'Hello World');
// Close the file
CloseFile(myFile);
// Reopen the file in read mode
Reset(myFile);
// Display the file contents
while not Eof(myFile) do
begin
ReadLn(myFile, data);
ShowMessage(data);
end;
// Close the file for the last time
CloseFile(myFile);
// Now delete the file
if DeleteFile(fileName)
then ShowMessage(fileName+' deleted OK')
else ShowMessage(fileName+' not deleted');
// Try to delete the file again
if DeleteFile(fileName)
then ShowMessage(fileName+' deleted OK again!')
else ShowMessage(fileName+' not deleted, error = '+
IntToStr(GetLastError));
end; |
|