首页 > 解决方案 > How to start explorer.exe with QProcess when the path to the specified file contains spaces?

问题描述

I want to start explorer and select a specific file. So I run

QProcess::startDetached(command);

with command set to

explorer.exe /select,C:\Users\....\file.txt

This works fine, but will fail if the path to the file contains spaces. But if I put the path in quotes

explorer.exe /select,"C:\Users\....\file.txt"

the explorer will open the documents folder and not the specified path. Running the same string from the command line works fine.

The string is initialized with

command = "explorer.exe" + "/select," + "\"" + QDir::toNativeSeparators(path) + "\"";

标签: c++qtqt5explorerqprocess

解决方案


如何做到这一点确实不是那么直观,但也不是不可能。

解决方案

将命令的所有参数 , 分解explorer.exe为单独的字符串,即/select, ,, the_path

例子

QProcess::startDetached("explorer.exe", QStringList{"/select", ",", "C:\\Users\\Your Username\\Desktop\\Folder With Spaces\\file.txt"});

推荐阅读