I have created a class which builds a Crystal report and displays it in a report viewer.
However, without the Crystal Redistributable, the code crashes. How can I programatically detect whether the end-user has the Crystal DLL installed?
The code I am required to include is:
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared
I suppose Crystal installs to GAC. So you need to programmatically browse Global assembly cache and look for Crystal assembly there
GAC can be programmatically managed thru Windows API. Here you will find a simple wrapper that allows to manage GAC from a managed code
But I would not recommend you to go this way. :)
The good solution is usually too check for all required assemblies during the installation of your software, I suppose Windows Installer has functions to check for assemblies
However, it is not always OK. Suppose if you would like users to run your program even without Crystal, but do not allow show reports if Crystal is not installed.
In this case you should decouple all Reports functionality from other forms. You should create a separate project with Crystal Reports functionality, let's call it MyReports. Only that project should reference to Crystal assemblies, while other should not. Other projects also should not directly reference to MyReports.
And finally you should use reflection to call a report viewer that is implemented inside MyReports from other projects.
Usually you will write try / catch around the procedure where you will use reflection to load MyReports assembly from file. So, if Crystal or something else not exist, it will throw exception, you will display it to user, but the other parts of the program will work.
Also, you can read about Dependency Injection design pattern, it helps to manage such problems.
Ideally, you would take care of all of the dependencies of your application at install time and make sure that all the the required components are already installed. However you can wrap your code that makes calls to the DLLs in question in a try..catch block and catch the DllNotFoundException when it gets thrown.
You could check if the following registry key exists
This will tell you if crystal reports is installed. if you want to check a particular version check the child key. For instance the crystal reports installed with Visual Studio 2008 is 10.2
You can find out these details by looking into the registry. I will search and provide an update.
Edit: You could also include Crystal reports as a prerequisite in the deployment package. You can then be sure that the final user has the library.