QCoreApplication获取信息
qDebug() << QCoreApplication::applicationFilePath();
qDebug() << QCoreApplication::applicationDirPath();
qDebug() << QCoreApplication::applicationPid();
qDebug() << QCoreApplication::applicationName();
创建文件相关
QString strDirPath = QCoreApplication::applicationDirPath();
strDirPath += "/myFolder";
QFileInfo fileInfo(strDirPath);
if(!fileInfo.exists()){
QDir dir("");
bool res = dir.mkpath(strDirPath);
qDebug() << "res:" << res;
}
QString fileName = strDirPath + "/test.txt";
QFile file(fileName);
bool delOld = false;
if(delOld){
if(!file.open(QFile::WriteOnly | QFile::Truncate)){
qDebug() << "open error!";
return 0;
}
}else{
if(!file.open(QFile::ReadWrite)){
qDebug() << "open error!";
return 0;
}
}
QByteArray byteArr = "hello,world!!";
qint64 ret = -1;
file.seek( file.size() );
ret = file.write( byteArr );
qDebug() << ret;
file.close();
查看所有文件大小
#include <QDir>
qint64 GetDirSize(const QString &path){
QDir dir(path);
qint64 size = 0;
foreach(QFileInfo fileInfo,dir.entryInfoList(QDir::Files))
{
size += fileInfo.size();
}
foreach(QString subDir,dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot))
{
size += du(path+QDir::separator()+subDir);
}
char unit ='B';
qint64 curSize=size;
if(curSize>1024){
curSize/=1024;
unit ='K';
if(curSize>1024){
curSize/=1024;
unit ='M';
if(curSize>1024){
curSize/=1024;
unit ='G';
}
}
}
qDebug()<<curSize<<unit<<"\t"<<qPrintable(path)<<endl;
return size;
}
查看指定目录指定类型的文件
#include <QDirIterator>
#include <QString>
#include <QDebug>
QString strDir = "D:/tools";
QString strFilter = "*.exe|*.msi";
QDirIterator dirIterator(strDir, strFilter.split("|"), QDir::Files);
while(dirIterator.hasNext()){
qDebug() << (dirIterator.next());
}