How to start explorer.exe with QProcess when the path to the specified file contains spaces?

Viewed 316

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) + "\"";
1 Answers

How to achieve this is indeed not so intuitive, but also not impossible.

Solution

Decompose all arguments of the command, explorer.exe, as separate strings, i.e. /select, ,, the_path.

Example

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