是否有一种非 Java 跨平台方式来启动特定文件类型的关联应用程序?
首先,我发现了一些 < a href="https://stackoverflow.com/questions/526037/java-how-to-open-user-system-prefered-editor-for-given-file">java 特定问题 和答案。我正在寻找更多“本机”但跨平台的解决方案,使用 C、C++、某种 shell 脚本,或者就我而言,使用 Qt。
所以问题是,是否有标准的、跨平台的方法来以编程方式打开某些文件类型的关联应用程序。或者至少查明是否有关联的应用程序并能够找到并启动它们?
我所说的跨平台是指 Windows、OSX 和 linux (gnome/kde)。用例是拥有一个数据库,其中存储的文件为 blob,将在三个不同的目标上读取。
First, I found a couple of java specific questions and answers for this. I am looking for more "native", but cross platform solution, using C, C++, some kind of shell scripts, or, in my case, Qt.
So the question is, are there standard, cross platform, ways to programmatically open the associated application for certain file types. Or at least to find out if there are associated applications and be able to locate and launch them?
By cross platform I mean Windows, OSX and linux (gnome/kde). The use case is having a database with stored files as blobs that will be read on the three different targets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道有什么跨平台的方式。
在 Windows 中,有
start
命令,它将启动关联的默认应用程序。 (例如start foo.doc
将启动默认的 Word 文档编辑器,启动 http://StackOverflow.Com/
默认的 Web 浏览器并启动 mailto:[email protected]
默认邮件应用程序。)在 OS X 中是
open
命令,它做同样的事情。Linux 只是一个操作系统内核。操作系统内核不知道有关“文件类型”或“MIME 类型”或“关联应用程序”或类似内容的任何信息。因此,对于 Linux 来说,这样的东西根本不可能存在。
Freedesktop Group 有一个
xdg-open
命令的规范< /a>,适用于所有 Freedesktop 兼容的图形桌面(无论是 Linux、FreeBSD、NetBSD、OpenBSD、DragonflyBSD、OpenSolaris 还是其他桌面)。但是,显然不能保证它在非 Freedesktop 系统上工作,并且当然不能保证在非图形系统上工作。在所有三种情况下,这是一个命令行应用程序,而不是 C 或 C++ API,但您显然可以通过
system
调用它。I don't know of any cross-platform way.
In Windows, there is the
start
command, which will launch the associated default application. (E.g.start foo.doc
will launch the default Word document editor,start http://StackOverflow.Com/
the default web browser andstart mailto:[email protected]
the default mail app.)In OS X there is the
open
command, which does the same thing.Linux is just an Operating System kernel. OS kernels don't know anything about "filetypes" or "MIME types" or "associated applications" or anything like that. Therefore, such a thing simply cannot exist for Linux.
The Freedesktop Group has a specification for an
xdg-open
command, which works on all Freedesktop-compliant graphical desktops (be they Linux, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, OpenSolaris or otherwise). However, it is obviously not guaranteed to work on non-Freedesktop systems and it is certainly not guaranteed to work on non-graphical systems.In all three cases, this is a command line application, not a C or C++ API, but you can obviously call it via
system
.既然您已经注意到您正在使用 Qt,那么值得一提的是 QDesktopServices 类,尤其是 < href="http://doc.trolltech.com/qdesktopservices.html#openUrl" rel="noreferrer">openUrl(QUrl) 方法。它几乎可以在 Qt 支持的所有平台上执行您所描述的操作。
Since you have noted that you are using Qt, it's worth mentioning the QDesktopServices class, and especially the openUrl(QUrl) method. It does pretty much what you've described on all platforms supported by Qt.
例如,C 中有一个系统调用:
这将使用默认编辑器(在我的例子中为 Visual Studio)打开文件。我不确定Linux和Mac,你可能需要在那里编写“open main.cpp”(这可以通过#ifdef构造来处理)。
There is the system call in C, for example:
This will open the file using the default editor (Visual Studio in my case). I'm not sure about Linux and Mac, you may need to write "open main.cpp" there (which can be taken care of by #ifdef constructs).