zoukankan      html  css  js  c++  java
  • Qt 删除目录

    删除目标的目录,若该目录下有子目录,一并删除。

     1 //判断是否存在子目录
     2 bool judgeDir(QDir dir)
     3 {
     4     dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
     5     foreach(QFileInfo fileInfo, dir.entryInfoList())
     6     {
     7         if(fileInfo.isDir())
     8         {
     9             return true;
    10         }
    11         else if(fileInfo.isFile())
    12         {
    13             return true;
    14         }
    15         else
    16         {
    17             return false;
    18         }
    19     }
    20     return true;
    21 }
    22 
    23 //删除目标目录
    24 void delDir(QString path)
    25 {
    26     if(path.isEmpty())
    27     {
    28         QMessageBox::warning(this,tr("警告"),tr("路径为空"),QMessageBox::Yes);
    29         return;
    30     }
    31     QDir dir(path);
    32 
    33     dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
    34     foreach(QFileInfo fileInfo, dir.entryInfoList())
    35     {
    36         if(fileInfo.isFile())
    37         {
    38             if(!fileInfo.isWritable())
    39             {
    40                 QFile file(fileInfo.absoluteFilePath());
    41                 file.setPermissions(QFile::WriteOwner);
    42             }
    43 
    44             fileInfo.dir().remove(fileInfo.fileName());
    45 
    46         }
    47         else if(fileInfo.isDir())
    48         {
    49             delDir(fileInfo.absoluteFilePath());
    50         }
    51     }
    52     dir.rmpath(dir.absolutePath());
    53 }
    54 
    55 //删除传入的目录
    56 void deleteFile(QString path)
    57 {
    58     QDir dir(path);
    59     int saveDel = QMessageBox::question(this,tr("提示"),tr("确定删除目录").arg(path), QMessageBox::Yes | QMessageBox::No );
    60    
    61     if(QMessageBox::Yes == saveDel)
    62     {
    63         QDir currentDir(path);
    64         if(judgeDir(currentDir))
    65         {
    66             delDir(currentDir.absolutePath());
    67         }
    68         else
    69         {
    70             dir.rmdir(path);
    71         }
    72     }
    73 }
  • 相关阅读:
    Eclipse 开发过程中利用 JavaRebel 提高效率
    数字转化为大写中文
    网页变灰
    解决QQ截图无法在PS中粘贴
    ORACLE操作表时”资源正忙,需指定nowait"的解锁方法
    网页常用代码
    SQL Server 2000 删除注册的服务器
    GridView 显示序号
    读取Excel数据到DataTable
    清除SVN版本控制
  • 原文地址:https://www.cnblogs.com/tyche116/p/8507833.html
Copyright © 2011-2022 走看看