4

I'm trying to open the editor associated with 'txt' files - here's the rub though, my file doesn't actually have the txt extension, so I can't just ShellExecute it, I have to find the associated program by hand.

To do this, I've been trying to use the IApplicationAssociationRegistration::QueryCurrentDefault COM method from Shell, but it returns a ProgId, which is less than useless to me. How can I map this to an EXE? I'm using C#, but it doesn't seem relevant.

Edit: The approach linked below where you directly access the Registry doesn't work in all cases anymore. The correct way is above, but I need the next step - how do I take the returned ProgId and get the associated program?

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 1
    Possible duplicate: http://stackoverflow.com/q/162331/1048330 – tenorsax Feb 03 '12 at 00:12
  • Good find, but these answers are incorrect as of >= Vista - I tried this approach previously, but it doesn't work; depending on how you set the association, this will return incorrect results – Ana Betts Feb 03 '12 at 00:17
  • 1
    That's an unproductive way to go about it. The one-and-only reason for picking a filename extension is its association. Pick .txt, problem solved. – Hans Passant Feb 03 '12 at 00:52
  • I don't get to choose the extension of the file, it's generated by another application I don't control – Ana Betts Feb 03 '12 at 00:55
  • I ended up working around this, but there has to be a *correct* answer to this. I can't believe in 2012, the correct answer is digging around in the Registry, that's no way to live. – Ana Betts Feb 07 '12 at 05:23

2 Answers2

7

To execute one file as if it were another type of file, call ShellExecute and set the the lpClass member of the SHELLEXECUTEINFO structure to the progid you wish the file were.

It is important to do it this way, because the default handler for text files is not necessarily a program with a command line. It might be a DDE server or a drop target or a context menu handler or a delegated execution handler.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
-3

Sadly yes, you will have to use the registry. That is how the registration works and how it is recorded in Windows. The link explains all the different entries that you can expect, how to translate the entry to the exe path, and links to your interface at the bottom as well.