【C++】Qt文件自动提取拷贝。QT获得appdata下的文件路径,配置文件读取

本文介绍了一个用于自动从APPDATA路径下提取日志文件的小程序。该程序通过读取配置文件来确定要提取的日志,并将这些日志复制到程序目录下的Log文件夹内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总是让客户去APPDATA路径下找日志,因为这个路径不需要什么管理员权限,有的人不知道在哪,找半天也找不到。所以写了个日志文件自动提取的小程序。提取完可以配合一些压缩库将文件压缩

#include <QtCore/QCoreApplication>
#include <QStandardPaths>
#include <QFile>
#include <QSettings>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //获得程序运行目录,读取同目录下的配置文件,这里日志是appdata/local和appdata/roaming都有
    QString exePath = QCoreApplication::applicationDirPath();
    QSettings* pathsettings = new QSettings(exePath.append("/config.ini"), QSettings::IniFormat);
    pathsettings->beginGroup("Config");
    int localnum = pathsettings->value("Local_Max").toInt();
    int roamingnum = pathsettings->value("Roaming_Max").toInt();
    pathsettings->endGroup();

//将配置文件中填写的日志都拷贝到程序目录下的Log文件夹
    pathsettings->beginGroup("LocalPath");
    for (int i = 0; i < localnum; i++)
    {
        exePath = QCoreApplication::applicationDirPath();
        QString tmpLocal = QString("log%1").arg(i);
        QString tmpLogName = pathsettings->value(tmpLocal).toString();
        QStringList tmpList = QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation);
        QString tmpStr = tmpList[0];// appdata/local
        tmpStr.append("/");
        tmpStr.append(tmpLogName);
        QFile  tmpFile(tmpStr);
        if (tmpFile.exists())
            tmpFile.copy(exePath.append("/Log/%1").arg(tmpLogName));
    }
    pathsettings->endGroup();

    pathsettings->beginGroup("RoamingPath");
    for (int i = 0; i < roamingnum; i++)
    {
        exePath = QCoreApplication::applicationDirPath();
        QString tmpLocal = QString("log%1").arg(i);
        QString tmpLogName = pathsettings->value(tmpLocal).toString();
        QStringList tmpList = QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation);
        QString tmpStr = tmpList[0];// appdata/local
        tmpStr.chop(5);
        tmpStr.append("Roaming");
        tmpStr.append("/");
        tmpStr.append(tmpLogName);
        QFile  tmpFile(tmpStr);
        if (tmpFile.exists())
            tmpFile.copy(exePath.append("/Log/%1").arg(tmpLogName));
    }
    pathsettings->endGroup();

    printf("\n\n\n=======日志已全部收集到Log文件夹下======\n\n\n");
    system("pause");
    //return a.exec();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值