简体   繁体   中英

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

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, ie /select , , , the_path .

Example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM