6

I have a Help function in my Application, that consists of one webbrowser control. That webbrowser control gets filled with a .pdf file, the source for that .pdf file is our own website.

The problem is, that not everyone will have a PDF Reader installed on their machine, so I want to check whether one is installed: Yes or No. I searched the internet and I mostly saw that users on Stackoverflow where wanting to check if Adobe Reader was installed, that is not what I want. I need to know IF there is a PDF Reader somewhere installed on the machine.

I did find the following code, that can possibly help me:

public void CheckPdfReaderAvailable()      
{      
    RegistryKey key = Registry.ClassesRoot.OpenSubKey(".pdf");      
    Assert.IsNotNull(key);      
}  

As I look at the above code, my thoughts are that the code checks if the registry does know the .pdf format, but I'am not sure.

Can somebody tell me how to use the code above or provide me an example, about how I should take down this problem?

Thanks in advance!

EDIT:

The following answer helped my out: https://stackoverflow.com/a/774482/1661209

Another way to solve this problem, is to add a pdf reader lite to the prerequisites and make the users install that first, you don't have to check for a pdf Reader, because you know one is installed then, if it isn't you could say it is the mistake of the user that they can't use the help function, because you offered them a way to install the pdf reader easily using the published project.

Community
  • 1
  • 1
Max
  • 12,622
  • 16
  • 73
  • 101
  • Why not have HTML in a WebBrowser control? Why not just throw the PDF at the user and let them deal with it? What do you do if they don't have a PDF viewer installed? – Bart Friederichs Apr 12 '13 at 07:23
  • That is my problem here, I want to check if a user has a PDF viewer installed: Yes or No, if not I will provide them a download link, so they can still make use of my help function. – Max Apr 12 '13 at 07:25
  • Check this answer maybe it can help you or open a path for you [http://stackoverflow.com/questions/6086973/how-can-i-programmatically-check-file-that-a-file-association-exists-before-atte/6087057#6087057][1] [1]: http://stackoverflow.com/questions/6086973/how-can-i-programmatically-check-file-that-a-file-association-exists-before-atte/6087057#6087057 – Mahmut EFE Apr 12 '13 at 07:26
  • Thanks for the links you provided, would it be better to set a PDF Reader as prerequisites?(I'am using clickonce) – Max Apr 12 '13 at 07:29
  • Even if a program registers for PDF files, it may not support being hosted in a webbrowser control. See http://stackoverflow.com/questions/9227720/how-can-i-support-in-browser-display-of-a-pdf-file-in-internet-explorer-64-bit – Sheng Jiang 蒋晟 Apr 12 '13 at 19:44

3 Answers3

5

Apart from whether it is useful to know or not, you could probable check the following registry key:

HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/pdf

This will have an entry CLSID, which points to the class ID of the default application.

If the registry key or CLSID value is not present, then the MIME type is unknown, or there is no default application to handle the MIME type application/pdf files.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
2

You can query the registry directly but the recommended solution is to use the IQueryAssociations interface to see if there is a program registered to open pdf's. An example can be found on pinvoke.net.

nblackburn
  • 338
  • 2
  • 10
0

C# implementation of the approach suggested by John Willemse (won't recognize Edge as default viewer on non-N version of Windows 10) :

    private bool CanOpenPDFFiles
    {
        get
        {
            bool CLSIDpresent = false;

            try
            {
                using (Microsoft.Win32.RegistryKey applicationPDF = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\application/pdf"))
                {
                    if (applicationPDF != null)
                    {
                        var CLSID = applicationPDF.GetValue("CLSID");
                        if (CLSID != null)
                        {
                            CLSIDpresent = true;
                        }
                    }
                }
            }
            catch (Exception)
            {

            }

            return CLSIDpresent;
        }
    }
Florian Straub
  • 826
  • 9
  • 18