Qt5, symbolic link to a folder

Deadly 提交于 2019-12-11 13:30:23

问题


A dupe-ish question of this question, which (possibly) has got an outdated answer, as I can't get it to work in Qt5.

I wish to create a symbolic link to a folder for a result similar to QFile::link(). Given that QDir doesn't have an equivalent function, QProcess (or an external library) seems like the way out if I'm up to snuff. How would this be managed in Qt5?

Big thanks in advance.


回答1:


There are shortcuts and hardlinks on Windows. I think mklink refers to hardlinks.

It works for shortcuts:

#include <QCoreApplication>

#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile dir("D:\\source-dir");
    bool ok = dir.link("D:\\target-dir.lnk");

    if (ok)
    {
        qDebug() << "yeah!";
        return 0;
    }
    else {
        qDebug() << "Did not work :(";
        return 1;
    }
}

In this case you will find a shortcut in the Explorer but you cannot access the file D:\source-dir\Bitmap.bmp by typing D:\target-dir\Bitmap.bmp




回答2:


I found out that it cannot be done in Qt, so I ended up using the Win32 API instead. Specifically the CreateSymbolicLink() function.



来源:https://stackoverflow.com/questions/30270848/qt5-symbolic-link-to-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!