【问题标题】:If directory contains exe or msi file, start it with command line arguments如果目录包含 exe 或 msi 文件,请使用命令行参数启动它
【发布时间】:2015-01-27 23:00:06
【问题描述】:

此应用仅适用于 Windows。它将在 QT Creator 中构建。我以7zip.exe 为例,因为它非常快速且易于测试。 我有一个目录列表,每个目录都包含一个 *.exe*.msi 文件。 在Qt 内的pushbutton_clicked() 上,我想转到我指定的单个目录并启动该目录中的任何可执行文件或*.msi 文件。 *.exe*.msi 文件名会不时更改,否则我可以简单地使用系统命令。

system("启动 7zip\7zip.exe /S");

我的问题是我想运行通配符,例如*.exe*.msi 并为其添加命令行开关。

我现在想执行路径中的单个文件并添加参数/S

我在一个批处理文件中工作:

for /F %%a in ('dir /b 7zip\*.exe') do SET app1=%%~na

%app1% /S

但不确定如何在Qt 中实现它。

谢谢

【问题讨论】:

    标签: qt file command switch-statement exe


    【解决方案1】:

    您想使用两件事:QDirIterator 迭代系统中的目录,QProcess 使用参数启动外部进程。

    【讨论】:

      【解决方案2】:

      感谢 AlexanderVX

      它们正是我们所需要的。我敢肯定有很多方法可以让这更优雅,但它正是我现在想要做的。

          void MainWindow::on_pushButton_clicked()
      {
          //set initial directory to search for the exe file
          QDirIterator file("7zip", QDirIterator::Subdirectories);
          while (file.hasNext())
          {
              file.next();
              QString prog = file.fileName(); //get actual filename
              QStringList cswitch;
              cswitch << "/S";  //create the command switch to use when running the exe
              QProcess *install = new QProcess(this);  // create new process called install
              QDir::setCurrent("7zip"); //set working directory needed to install the exe
              install->start(prog, cswitch); //launch the exe with the desired command switch
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2021-04-30
        • 1970-01-01
        • 2015-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多