如何判断文件是否已经被打开?
在这里通过文件的一些属性实现判断文件是否被打开,通过QFile将文件尝试实现例如linux的move操作和rm -r 的操作,就可以判断是否文件被占用。
首先添加#include"QFile"头文件,再设置全局的判断标志位:
#include <QFile>
#include <QDesktopServices>
#include"qdebug.h"
#include <windows.h>
#include <QDir>
#include"QProcess"
#include"QFile"
enum Except{ EXCEP_ZERO,EXCEP_ONE};
bool fileNoUsed = false;
下面是具体的判断代码:
bool openDocumentation(){
QString runPath = QCoreApplication::applicationDirPath();
QString hglpName = "高质量C++和C编程指南.pdf";
QString hglpPath = QString("%1/%2").arg(runPath).arg(hglpName);
QString filePath = "file:///" + hglpPath;
QString fileName = hglpName;
QString newName = "./test01/" + hglpName;
if(QFile::exists(fileName) && QFile::exists("./test01/")){
if(QFile::copy(fileName, newName)){
if(QFile::remove(fileName)){
fileNoUsed = true;
}
}
}
if(fileNoUsed){
QFile::copy(newName, fileName);
QFile::remove(newName);
QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
}else{
QFile::remove(newName);
qDebug() << "++++++++++++6666666666666 filename No exit or has been open file!+++++++++++++++";
}
return fileNoUsed;
}
通过调用对结果判断即可。