qt 判断文件是否存在

1. 判断文件夹是不是存在

复制代码
 1 QString fullPath;//文件夹全路径
 2 /*方法1*/
 3 bool isDirExist(QString fullPath)
 4 {
 5     QDir dir(fullPath);
 6     if(dir.exists())
 7     {
 8       return true;
 9     }
10     return false;
11 }
12 /*方法2*/
13 bool isDirExist(QString fullPath)
14 {
15     QFileInfo fileInfo(fullPath);
16     if(fileInfo.isDir())
17     {
18       return true;
19     }
20     return false;
21 }
View Code
复制代码

2. 判断文件是不是存在

复制代码
 1 QString fullFileName;//文件全路径(包含文件名)
 2 /*方法1*/
 3 bool isFileExist(QString fullFileName)
 4 {
 5     QFileInfo fileInfo(fileFullName);
 6     if(fileInfo.isFile())
 7     {
 8         return true;
 9     }
10     return false;
11 }
View Code
复制代码

3、判断文件或文件夹是不是存在(即不确定字符串是文件还是文件夹路径)

复制代码
 1 QString fullFilePath;//路径名
 2 /*方法1*/
 3 bool isFileExist(QString fullFilePath)
 4 {
 5     QFileInfo fileInfo(fullFilePath);
 6     if(fileInfo.exists())
 7     {
 8         return true;
 9     }
10     return false;
11 }
12 /*方法2*/
13 bool isFileExist(QString fullFilePath)
14 {
15     QFile file(fullFilePath);
16     if(file.exists())
17     {
18         return true;
19     }
20     return false;
21 }
View Code
复制代码

4、判断文件夹是否存在,不存在则创建

复制代码
 1 /*方法1*/
 2 bool isDirExist(QString fullPath)
 3 {
 4     QDir dir(fullPath);
 5     if(dir.exists())
 6     {
 7       return true;
 8     }
 9     else
10     {
11        bool ok = dir.mkdir(fullPath);//只创建一级子目录,即必须保证上级目录存在
12        return ok;
13     }
14 }
15 
16 /*方法2*/
17 bool isDirExist(QString fullPath)
18 {
19     QDir dir(fullPath);
20     if(dir.exists())
21     {
22       return true;
23     }
24     else
25     {
26        QDir dir1;
27        bool ok = dir1.mkpath(fullPath);//创建多级目录
28        return ok;
29     }
30 }
View Code
复制代码

5、以下为摘录的其他网络测试代码

复制代码
{
    QFileInfo fi("C:/123");                     // 目录存在
    qDebug() << fi.isFile();                    // false
    qDebug() << fi.isDir();                     // true
    qDebug() << fi.exists();                    // true
    qDebug() << fi.isRoot();                    // false
    qDebug() << QFile::exists("C:/123");        // true
    qDebug() << QDir("C:/123").exists();        // true

    fi.setFile("C:/ABC");                       // 目录不存在
    qDebug() << fi.isFile();                    // false
    qDebug() << fi.isDir();                     // false
    qDebug() << fi.exists();                    // false
    qDebug() << fi.isRoot();                    // false
    qDebug() << QFile::exists("C:/ABC");        // false
    qDebug() << QDir("C:/ABC").exists();        // false

    fi.setFile("C:/");                          // 存在的驱动器
    qDebug() << fi.isFile();                    // false
    qDebug() << fi.isDir();                     // true
    qDebug() << fi.exists();                    // true
    qDebug() << fi.isRoot();                    // true
    qDebug() << QFile::exists("C:/");           // true
    qDebug() << QDir("C:/").exists();           // true

    fi.setFile("Z:/");                          // 不存在的驱动器
    qDebug() << fi.isFile();                    // false
    qDebug() << fi.isDir();                     // false
    qDebug() << fi.exists();                    // false
    qDebug() << fi.isRoot();                    // false
    qDebug() << QFile::exists("Z:/");           // false
    qDebug() << QDir("Z:/").exists();           // false

    fi.setFile("C:/123.lnk");                   // 快捷方式存在且指向的文件也存在
    qDebug() << fi.isFile();                    // true
    qDebug() << fi.isDir();                     // false
    qDebug() << fi.exists();                    // true
    qDebug() << fi.isRoot();                    // false
    qDebug() << QFile::exists("C:/123.lnk");    // true
    qDebug() << QDir("C:/123.lnk").exists();    // false

    fi.setFile("C:/456.lnk");                   // 快捷方式存在但指向的文件不存在
    qDebug() << fi.isFile();                    // false
    qDebug() << fi.isDir();                     // false
    qDebug() << fi.exists();                    // false
    qDebug() << fi.isRoot();                    // false
    qDebug() << QFile::exists("C:/456.lnk");    // false
    qDebug() << QDir("C:/456.lnk").exists();    // false
}
View Code
复制代码

可以看到,容易让人感到混乱的是exists方法,这个方法是通用的判断方法,可以看成是这样的表达式
exists() == (isFile() || isDir())

也就是说判断文件或文件夹是否存在单纯用exists方法是不严谨的
比如你的本意是判断文件是否存在,但文件不存在,而恰巧有个同名的文件夹,那么exists也会返回true。文件夹也是同理

根据上面的代码作出的一点总结

准确判断文件是否存在
1.用QFileInfo::isFile()方法

准确判断文件夹是否存在
1.用QFileInfo::isDir()方法
2.用QDir::exists()方法

不确定字符串是文件还是文件夹路径
1.用QFileInfo::exists()方法
2.用QFile::exists()方法

 

posted @   阳光下的小土豆  阅读(2449)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-12-20 Qt 遍历不规则树的节点
点击右上角即可分享
微信分享提示