1
 
 

在线客服

 
 

0755-8290-9500
7*24小时客服服务热线

 

八联企业QQ4008864086
八联总机:0755-8288-1000
24小时技术支持:0755-8290-9500

全国统一售前服务热线
八联网络科技全国统一售前咨询:400-886-4086
当前位置: 八联公告

请选择:

[ 技术支持 ] • 香港服务器Linux下使用合适程序打开文件
作者:客服中心 文章来源:八联网络科技 更新时间:2016-12-05

香港服务器Linux系统下编写界面程序时,有时候需要打开一个文件,并不是以简单的二进制方式或文本方式打开,要用当前系统中合适的应用程序打开,比如图片文件使用图片查看工具;压缩包使用归档压缩包管理工具;文本文件使用gedit等等。
下面介绍一个非常实用的工具:xdg-open
这是一个Linux下的脚本程序,使用它打开一个文件时,它会首先分析这个文件的类型,然后使用系统中注册好的与这个文件类型相符合的程序打开这个文件。
用法也很简单,Linux下打开一个终端,简单使用 xdg-open + file即可。
在程序中使用xdg-open也很简单,只需要结合system()这个函数即可。使用system + xdg-open方式打开的子进程会将其父进程设成init进程,即使我们的程序退出,它也不会受到影响。
man手册中关于xdg-open的介绍:
DG-OPEN(1)   xdg-open Manual   XDG-OPEN(1)
NAME 
xdg-open - opens a file or URL in the user's preferred application 
SYNOPSIS 
xdg-open {file | URL} 
xdg-open {--help | --manual | --version} 
DESCRIPTION 
xdg-open opens a file or URL in the user's preferred application. If a  
URL is provided the URL will be opened in the user's preferred web 
browser. If a file is provided the file will be opened in the preferred 
application for files of that type. xdg-open supports file, ftp, http 
and https URLs. 
xdg-open is for use inside a desktop session only. It is not 
recommended to use xdg-open as root. 
OPTIONS 
--help 
Show command synopsis. 
--manual 
Show this manualpage. 
--version 
Show the xdg-utils version information.
更多香港服务器知识,可登入八联网络www.8lian.cn官网进行了解。