想用QT编一段删除文件夹或文件的代码,网上搜索了很多,关于删除文件夹都用递归删除的方法,因为非空文件夹不能直接删除,只能先清空文件夹里的东西,才能执行删除文件夹的操作。实际上QT5之后有更简便的方法,就是用QDir::removeRecursively(),详细的可以查QT帮助文档。
  利用QDir::removeRecursively()和QFile::remove(),可以写出很简单的删除文件夹或文件的操作。
#include <QFile>
#include <QDir>
#include <QString>

 1 bool DeleteFileOrFolder(const QString &strPath)//要删除的文件夹或文件的路径
 2 {
 3     if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
 4         return false;
 5         
 6     QFileInfo FileInfo(strPath);
 7 
 8     if (FileInfo.isFile())//如果是文件
 9         QFile::remove(strPath);
10     else if (FileInfo.isDir())//如果是文件夹
11     {
12         QDir qDir(strPath);
13         qDir.removeRecursively();
14     }
15     return true;
16 }

 

相关文章:

  • 2021-10-02
  • 2022-01-31
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
猜你喜欢
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2021-06-01
相关资源
相似解决方案