当指定文件的路径包含空格时,如何使用QProcess启动explorer.exe? [英] How to start explorer.exe with QProcess when the path to the specified file contains spaces?

查看:62
本文介绍了当指定文件的路径包含空格时,如何使用QProcess启动explorer.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动资源管理器并选择一个特定的文件.所以我跑

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

QProcess::startDetached(command);

,其中 command 设置为

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"

资源管理器将打开documents文件夹,而不是指定的路径.从命令行运行相同的字符串可以正常工作.

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

该字符串用

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

推荐答案

如何实现这一目标确实不是那么直观,但也不是不可能.

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

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

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

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

这篇关于当指定文件的路径包含空格时,如何使用QProcess启动explorer.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆