c++ - 将带有斜杠的 Unix 路径转换为 ​​Windows 路径

标签 c++ windows qt unix

<分区>

我正在尝试运行命令以打开 explorer.exe 中的特定位置并选择特定文件:

QUrl url = QUrl::fromUserInput(file.absoluteFilePath());
QString str = "explorer.exe /select,\"" + url.toString() + "\"";
system(str.toStdString().c_str());

这适用于 Windows 位置。

但是因为我也从 NAS 和 MAC 用户打开位置,可以在那里创建文件夹,所以我遇到这样的文件夹问题:

//NAS/FOLDER/With/BackSLASH/file.ext

由于在 Mac 上可以使用斜杠来命名文件/文件夹,因此 system() 函数无法识别它,QString 将其转换为 U +002F.

在 Windows 资源管理器中显示为:

FOLDER•With•BackSLASH

而且,如果我使用命令提示符导航到这样的文件夹,它会显示如下文件夹名称:

PRODUCER'S NOTES 86

有没有人知道如何处理这种特殊情况或如何将此路径转换为可作为参数传递给 explorer.exe 的路径?

/* 编辑 08/31/2018*/

我将代码更改为:

#include <Shlobj.h>
#include <atlstr.h>
void exportManager::BrowseToFile(QString filename)
{
TCHAR tchar[512];
USES_CONVERSION;
_tcscpy(tchar, A2T(filename.toStdString().c_str()));

ITEMIDLIST *pidl = ILCreateFromPath(tchar);
 if (pidl) {
    SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
    ILFree(pidl);
 }
}
....
QString path = qFile.absoluteFilePath();
BrowseToFile(path.replace('/', '\\'));

但这仍然没有解决文件夹/文件名中反斜杠和斜杠的问题。 似乎是转换“filename.toStdString().c_str()”导致了问题。

如果我打印 (qDebug()) 路径,它看起来像这样: //NAS/文件夹\uF022With\uF022BackSLASH/file.ext

我发现了这个问题,这解决了我的问题。 QDesktopServices::openUrl with selecting specified file in explorer

最佳答案

您需要使用名为 SHOpenFolderAndSelectItems() 的函数通过命令行调用 explorer.exe。 这是执行此操作的示例代码:

void BrowseToFile(LPCTSTR filename)
{
    ITEMIDLIST *pidl = ILCreateFromPath(filename);
    if(pidl) {
        SHOpenFolderAndSelectItems(pidl,0,0,0);
        ILFree(pidl);
    }
}

希望对您有所帮助;干杯

关于c++ - 将带有斜杠的 Unix 路径转换为 ​​Windows 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52087117/

相关文章:

c++ - Qt 安装程序框架 : How to sign the maintenancetool. exe

c++ - 无法通过 QModelIndex 从 QTreeView 获取项目

c++ - 我写了一个简单的 vector 程序,其中我得到了以下输出。你能帮我理解它的输出吗?

c++ - 将文件保存到目录 C++

windows - 在 Babun (Windows 10) 上使用 pact 安装后,Cygwin 包无法正常工作

c++ - 更改 Windows 资源管理器中的当前路径

c++ - QT4 中的蓝牙连接?

c++ - 虚函数题

windows - Git 在 Windows 10 上运行缓慢

Qt3D "qt.glx: qglx_findConfig: Failed to finding matching FBConfig"警告