找回密码
 注-册

QQ登录

只需一步,快速开始

查看: 27446|回复: 0

Delphi里如何删除指定的文件夹和文件

[复制链接]
发表于 2005-11-2 00:13:12 | 显示全部楼层 |阅读模式
操作系统: 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;
您需要登录后才可以回帖 登录 | 注-册

本版积分规则

Archiver|手机版|小黑屋|数码鹭岛 ( 闽ICP备20006246号 )

counter

GMT+8, 2026-1-24 11:52 , Processed in 0.096332 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表