我如何在其默认的程序打开文件 - Linux的 [英] How do I open a file in its default program - Linux
问题描述
如何编程在其默认的程序在Linux中(IM使用Ubuntu 10.10)。打开一个文件
How do I programmatically open a file in its default program in Linux (im using Ubuntu 10.10).
例如,打开* .MP3将打开在影片播放器文件(或别的东西)。
For example, opening *.mp3 will open the file in Movie Player (or something else).
感谢您提前。
斯捷潘
推荐答案
您需要运行的的gnome-开放,KDE开,或外开,的具体取决于台式机所使用。
You need to run gnome-open, kde-open, or exo-open, depending on which desktop you are using.
我相信这是一个叫做项目的 XDG-utils的的试图提供一个统一的接口来本地桌面。
I believe there is a project called xdg-utils that attempts to provide a unified interface to the local desktop.
那么,是这样的:
snprintf(s, sizeof s, "%s %s", "xdg-open", the_file);
system(s);
谨防code注射液。它的安全绕过用户输入脚本层,因此考虑是这样的:
Beware of code injection. It's safer to bypass scripting layers with user input, so consider something like:
pid = fork();
if (pid == 0) {
execl("/usr/bin/xdg-open", "xdg-open", the_file, (char *)0);
exit(1);
}
// parent will usually wait for child here
这篇关于我如何在其默认的程序打开文件 - Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!