设为首页收藏本站

数码鹭岛论坛

 找回密码
 注-册

QQ登录

只需一步,快速开始

搜索
查看: 27315|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 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;
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 注-册

本版积分规则

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

counter

GMT+8, 2025-12-3 17:04 , Processed in 0.068275 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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