Linux下使用合适程序打开文件

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值