Script to associate an extension to a program
Asked Answered
F

4

11

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?

Floriculture answered 17/10, 2008 at 16:41 Comment(1)
Take a look at superuser.com/q/406985/8271E
B
17

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_ROOT\xmlfile". There you can see (and change) what command is going to be used to launch that type of file:

HKEY_CLASSES_ROOT\xmlfile\shell\open\command

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_ROOT\xmlfile\shell" 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_ROOT\xmlfile\shell\open\command" /ve /d "path\to\program %1"

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

Barrie answered 17/10, 2008 at 16:47 Comment(4)
In my pc this key points to "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSOXMLED.EXE" /verb open "%1" But maybe this .exe then open the OpenOffice calc app.Floriculture
It should be the OpenOffice Calc app, if you want to launch that. "MSOXMLED.EXE" really is the MS Office XML editor that comes with InfoPathBarrie
Not sure if this is the key. If I delete it, the doble click still works.Floriculture
Hi! I used SharpShell and created an item in explorer context menu and when I (user) right click on some song files and click on my added item those files will add to my music player. now i want to do it by double-clicking (opening,press enter )files too. how can i do it? do you know? can you help me please?Fakery
E
2

consider the dos command assoc:

C:>assoc /? Displays or modifies file extension associations

ASSOC [.ext[=[fileType]]]

.ext Specifies the file extension to associate the file type with fileType Specifies the file type to associate with the file extension

Type ASSOC without parameters to display the current file associations. If ASSOC is invoked with just a file extension, it displays the current file association for that file extension. Specify nothing for the file type and the command will delete the association for the file extension.

Explain answered 17/10, 2008 at 17:3 Comment(3)
Can assoc also change the associated application? Has it been there always or is it new to XP or something? It's at least new to me...Barrie
"Assoc .xml" returns ".xml=xmlfile" "Assoc xmlfile" returns "xmlfile=XML Document" Can't go further because Assoc do not accept the spaceFloriculture
@Eduardo You should be able to quote the space.Explain
W
1

Using file associations in this case seems like the wrong thing to do. You want your application to open the file in OpenOffice but what if your user wants to leave the file association for XML files untouched? What if something else on their system also relies on that association? You are breaking their system in that case. If you are the IT person then perhaps that is OK (still questionable programming practice), but if not then this is a bad thing to do.

Use the OpenOffice COM implementation to open the file instead.

Good simple example here: http://www.kalitech.fr/clients/doc/VB_APIOOo_en.html

Wiper answered 18/9, 2012 at 17:32 Comment(0)
U
-1

I just came across this whilst searching for the same answer. I found a better solution using the Windows FindExecutable API, that can be used from C# using PInvoke.

http://www.pinvoke.net/default.aspx/shell32.findexecutable

Update answered 15/9, 2010 at 15:39 Comment(1)
On further investigation it's better to use AssocQueryString which doesn't require a phyisical file for the lookup:pinvoke.net/default.aspx/shlwapi/AssocQueryString.htmlUpdate

© 2022 - 2024 — McMap. All rights reserved.