I want to get the program associated with an extension, in both Linux and Windows. I know that I can execute xdg-open in Linux or start in Windows, but I'm gonna open a media file on a remote server, so using this method would open the web browser, not the default media player. How can I get the default application for an extension?
解决方案
There is a program called xdg-mime that's like xdg-open, but prints the information rather than trying to open the file.
xdg-mime query filetype /tmp/foobar.png
Prints the MIME type of the file /tmp/foobar.png, in this case image/png
xdg-mime query default image/png
Prints the .desktop filename of the application which is registered to open PNG files.
On Windows, if you can't find an API, maybe you can use assoc or ftype, which are part of cmd.