How can I programmatically check file that a file association exists before attempting to Process.Start() it?

百般思念 提交于 2019-12-09 16:26:21

问题


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


回答1:


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#




回答2:


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}"


来源:https://stackoverflow.com/questions/6086973/how-can-i-programmatically-check-file-that-a-file-association-exists-before-atte

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