Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

file - Script to associate an extension to a program

My customer is replacing MS Office with OpenOffice in some workstations. My program export a file to Excel using the .xml extension (using open format) and opens it using the current associated program (using ShellExecute)

The problem is that OpenOffice does not register the .xml extension associated with it.

Manually association works fine, but I want to make a .reg or something to easily change the setting.

I'm looking in the registry in a PC with the change already made, but the

"HKEY_CLASSES_ROOT.xml" 

key does not have anything referencing OpenOffice.

Where is the association stored? How can I make a script to do the work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The real association is stored in the key that "HKEY_CLASSES_ROOT.xml" points to.

On my machine, the default value of that key says "xmlfile", most likely that is the same for yours.

So let's go to "HKEY_CLASSES_ROOTxmlfile". There you can see (and change) what command is going to be used to launch that type of file:

HKEY_CLASSES_ROOTxmlfileshellopencommand

Windows uses this kind of redirection to map multiple file extensions to the same file type, and thus to the same application.

Under "HKEY_CLASSES_ROOTxmlfileshell" there are multiple sub-keys that resemble the "verbs" of what you can do to the file. Again, the default value of the "shell" key decides which of these verbs is used if you double click the file. In my case this is "open".

Conclusion:

With that knowledge, the easiest way to make an association scriptable is to use regedit to export a .reg file containing that change, and apply it to the target computer with a double click or:

regedit /s new_xml_association.reg

or (if you are on XP or higher and know what you do) overwrite the current value with:

reg add "HKEY_CLASSES_ROOTxmlfileshellopencommand" /ve /d "pathoprogram %1"

At any rate, a deeper look into reg add/? command is advised. The first solution is safer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...