8

My .Net Windows Forms application generates a PDF report, which I want to show to the user.

Instead of just assuming the client PC has a PDF viewer installed and blindly throwing the PDF at the Operating System to open, is there a way to check against the list of file associations on the client beforehand, then show a "you need a PDF viewer application installed - here's a couple of suggestions.." dialog if PDF isn't a registered type?

I've found a lot of questions and answers about changing or registering file associations, but I just want an easy way to query the list, not change it.

I'd like a solution that works on Windows XP onwards (WinXP, Vista, Win7).

Thanks for your help

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
Andrew
  • 12,991
  • 15
  • 55
  • 85
  • Windows will show that exact dialog automatically if there is no registered handler. Don't reinvent the wheel within your application. – Cody Gray - on strike May 22 '11 at 09:08
  • 1
    @Cody, that's a very good point, and one I would (by default), normally agree with. In this case, I think the windows dialog doesn't really explain that *my* application is trying to show a report - I don't think it's explicit enough e.g: http://windowsxp.mvps.org/images/webservice.JPG – Andrew May 22 '11 at 09:22
  • 1
    Yeah, I was assuming you were running a modern version of Windows. The dialog has improved substantially. It's much more user-friendly. Still, I'd stick with the standard dialog. If the users are still running an early version of the OS, that's the dialog they're familiar with. Showing a custom one is more likely to confuse them, no matter how much time you spend designing it. – Cody Gray - on strike May 22 '11 at 09:24

2 Answers2

9

It's better not use the registry directly but rely on the Windows API instead. Here is a link on SO that gives a .NET solution: How do I get File Type Information based on extension? (not MIME) in c#

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
0

I found there is a .pdf key in the registry, maybe it can help you:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.pdf]
"Content Type"="application/pdf"
@="AcroExch.Document"

[HKEY_CLASSES_ROOT\.pdf\OpenWithList]
@=""

[HKEY_CLASSES_ROOT\.pdf\OpenWithList\AcroRd32.exe]
@=""

[HKEY_CLASSES_ROOT\.pdf\ShellEx]

[HKEY_CLASSES_ROOT\.pdf\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}]
@="{DC6EFB56-9CFA-464D-8880-44885D7DC193}"
Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
  • I get the suspicion that you found this by opening Registry Editor and running a search for a keyword. That makes my skin crawl; [it's *always* the wrong way of figuring out how to do something in Windows](http://blogs.msdn.com/b/oldnewthing/archive/2003/11/03/55532.aspx). In this case, you narrowly escape a downvote, because editing the registry actually *is* the documented way of altering file associations. But that's *extremely* rare, so this shouldn't become a habit. [end rant] – Cody Gray - on strike May 22 '11 at 09:28
  • @Cody Gary, I don't think Marino was suggesting editing the registry but that is not clear. I wonder if there is a better way. – Jodrell May 22 '11 at 09:32