简体   繁体   English

当指定文件的路径包含空格时,如何使用 QProcess 启动 explorer.exe?

[英]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 .将命令explorer.exe所有参数分解为单独的字符串,即/select,the_path

Example例子

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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