软件注册站
热情软件屋

 
如何删除指定的文件夹和文件
编号: QA004814    
建立日期: 2003年12月27日 最后修改日期: 2003年12月27日
所属类别: Delphi - 磁盘、文件和目录
   
    操作系统: 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;
    

    
    相关问题:
    QA002594 "如何将文件删除到回收站"

    

此问题由李海回答。

附加关键字:编程, 源程序, programming, source code, Delphi, VCL, Borland, 磁盘、文件和目录, disk, file, fat, directory, folder

   
 
把这个问题推荐给朋友
   
 
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio