标题说明了一切。我见过很多执行相反操作的解决方案,但不是这种方式。
我正在使用 Qt 创建临时文件。如果我知道它的名字,它会是这样的:
QTemporaryFile tempFile;
tempFile.open();
qDebug() << tempFile.fileName();
// C:/Users/USERNA~1/AppData/Local/Temp/qt_temp.Hp4264
我尝试过一些解决方案,例如使用 QFileInfo:
QFileInfo fileInfo(tempFile);
qDebug() << fileInfo.filePath()
和 QDir:
QDir dir(tempFile.fileName());  
qDebug() << dir.absolutePath();
没有成功。
有没有纯粹使用 Qt 的解决方案?我知道我可以导航目录树来查找全名,但我试图避免这种情况,特别是因为两个文件夹可能具有相同的前缀。
win32 函数GetLongPathName就是您的答案。
QString shortPath = tempFile.fileName();
int length = GetLongPathNameW(shortPath.utf16(),0,0);
wchar_t* buffer = new wchar_t[length];
length = GetLongPathNameW(shortPath.utf16(), buffer, length);
QString fullpath = QString::fromUtf16(buffer, length);
delete[] buffer;
没有纯粹的 Qt 函数来实现这一点,因为只有 Windows 才能做到这一点(并且 Qt 应该是可移植的)