我必须构建一个小对话框来创建指向文件夹的符号链接(symbolic link)。
在 Windows 中我会使用 mklink/D 命令。
有没有可能在 Qt 中创建这样的链接?我只看到 QFile 在文件之间创建链接并且它们需要以 .lnk ( http://qt-project.org/doc/qt-4.8/qfile.html#link ) 结尾 另一方面,QDir 不提供任何内容。
有什么建议吗?
最好的问候, 理查德
最佳答案
Is there a possibility to create such links in Qt?
是的,它是,但仅限于 Unix。
不幸的是,Windows 上的 QFile 不支持它,甚至 QDir 也不支持。在我看来,这对于提交有关 Qt Bug tracker 的报告将是一个有用的功能。 .
解决方法是这样写:
#ifdef Q_OS_UNIX
QFile::link(sourceDir.absolutePath(), destDir.absolutePath());
#elif Q_OS_WIN
QProcess process;
process.start("mklink /D");
// Wait for it to start
if(!process.waitForStarted())
return 0;
bool retval = false;
QByteArray buffer;
while ((retval = process.waitForFinished()));
buffer.append(process.readAll());
if (!retval) {
qDebug() << "Process error:" << process.errorString();
qDebug() << "Output:" << buffer;
return 1;
}
#endif
关于c++ - Qt 创建文件夹之间的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21011063/