Get Windows files associations via Java

折月煮酒 提交于 2019-12-24 10:18:17

问题


I thought this would be an easy task but . . . I want my Java program to get the user's Windows file associations. In other words, I want to know what the user uses to open .txt files, .cvs files, etc. The assoc and ftype commands provide that info, but not for the user. In other words, if I've set my text editor to Notepad++, assoc and ftype don't show it. They show the system default, Notepad, instead. It looks like I have to get that info from the registry but I have two problems. 1) I don't know the exact registry keys I want to pull (though I've looked at "reg query HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, etc.) 2) I don't know how to pull the key from the registry. I've seen JNI mentioned but haven't figured out the details. Any hints appreciated.


回答1:


In Win7, you can find the "class"* for each file extension in the

HKLM\SOFTWARE\Classes\<extension>\(Default)

key.

For example, on my machine

HKLM\SOFTWARE\Classes\.txt

has a (Default) key of txtfile.

In that same path, you can find what opens files of the class txtfile:

HKLM\SOFTWARE\Classes\txtfile

Which should have a subpath of ...\shell\open\command

On my system,

HKLM\SOFTWARE\Classes\txtfile\shell\open\command\(Default)

is

%SystemRoot%\system32\NOTEPAD.EXE %1

Which you could parse to find the executable that opens .txt files.

For user-specific customizations, you can replace

HKLM\SOFTWARE\Classes\

with

HKCU\Software\Classes\

All that being said, I like MadProgrammer's suggestion if possible for your application.

* I'm sure there is a better name for "class", I just don't know it.




回答2:


Take a look at the Eclipse's Program class, which should to what you want: http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fprogram%2FProgram.html.

If you would like to launch the program, use the Desktop-class (like MadProgrammer suggested)



来源:https://stackoverflow.com/questions/13576124/get-windows-files-associations-via-java

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