Is there a non-java, cross platform way to launch the associated application for a certain file type?

扶醉桌前 提交于 2019-12-05 02:44:13

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 and start mailto:mail@example.com 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.

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.

There is the system call in C, for example:

system("main.cpp");

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).

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