Qt5中如何执行.cmd文件

How to execute .cmd fille in Qt5

如何使用此命令执行 .cmd 文件:

TASKKILL /F /PID 14364
MOVE /Y "C:/Users/BBCCA/AppData/Roaming/DWAKU2\DWAKU2.exe" "D:/DWAKU2/build-    DWAKU2-Desktop_Qt_5_15_2_MinGW_64_bit_Static-Release/release"
START "" "D:/DWAKU2/build-DWAKU2-Desktop_Qt_5_15_2_MinGW_64_bit_Static-Release/release/DWAKU2.exe"

如何在 qt 中使用命令控制台执行此脚本?
如您所见-第一个命令关闭 qt 应用程序。


首先,我尝试使用QProcess来执行其中的所有命令。

auto programmName = QFileInfo(QCoreApplication::applicationFilePath()).fileName();
QProcess consola;
QString command = "cmd";
QStringList commandArgs;
QString subcommand1, subcommand2;
subcommand1 += "TASKKILL /IM ";
subcommand1 += programmName;
subcommand2 += "DEL /Q ";
subcommand2 += QCoreApplication::applicationFilePath();
commandArgs << "/c" << subcommand1 << "&&" << subcommand2;
qDebug() << commandArgs;
consola.startDetached(command, commandArgs);
consola.waitForFinished();

但是没有执行错误:

Ошибка: Неправильный параметр или аргумент - '/Q'. Введите "TASKKILL /?" для получения справки по использованию.

翻译:

Error: Incorrect parameter or argument - '/Q'. Enter "TASKKILL /?" for help on how to use it.

在大量阅读文档和询问后 ru.stuckoverflow - 放弃了这个想法并尝试使用 cmd 文件。
你可以在上面阅读。

此文件的执行是:

// Make updater script
    if (fileCorrect){
        ui->LoadingAnimationLabel->setText(bLText + "Making updater script... " + aLText);
        QFile file(appDataPath + QDir::separator() + "updater.cmd");
        if (file.open(QIODevice::WriteOnly)) {

            auto pid = "TASKKILL /F /PID " + QString::number(QCoreApplication::applicationPid()) + "\r\n";
            auto move = "MOVE /Y \"" + appDataPath + QDir::separator() + QFileInfo(QCoreApplication::applicationFilePath()).fileName() + "\" \"" + QCoreApplication::applicationDirPath() + "\"\r\n";
            auto start = "START \"\" \"" + QCoreApplication::applicationFilePath() + "\"\r\n";

        file.write(pid.toLocal8Bit());
        file.write(move.toLocal8Bit());
        file.write(start.toLocal8Bit());

        } else fileCorrect = false;
        file.close();
    }

    // Installing updates
    if (fileCorrect){
        ui->LoadingAnimationLabel->setText(bLText + "Installing update files... " + aLText);
        auto command = appDataPath + QDir::separator() + "updater.cmd";
        QProcess updater;
        updater.startDetached(appDataPath + QDir::separator() + "updater.cmd");
    }

当程序执行此文件时 - 只执行了第一条命令。
但是当我试图通过单击它来执行这个文件时 - 它工作正常....

看看 QProcess,尤其是 startDetached 方法。根据 docs,“如果调用进程退出,分离进程将继续 运行 不受影响。”

    QProcess *myProcess = new QProcess();
    myProcess->setProgram("updater.cmd");
    myProcess->startDetached();